Get only the file name, excluding the extension
example.java
import java.io.File;
String fullPathString = "/home/hoge/abc.txt";
public static String getFileName(final String fullPathString) {
File file = new File(fullPathString);
String basename = file.getName();
String woext = basename.substring(0,basename.lastIndexOf('.'));
System.out.println(woext);
}
String basename = file.getName();
Get the file name with the getName () method
String woext = basename.substring(0,basename.lastIndexOf('.'));
Get from the first argument to the second argument character with the substring () method Check the number of the argument ('.') Counting from the end with the lastINdexOf () method
You should now see abc
https://java-code.jp/187 https://java-code.jp/795 http://simplesandsamples.com/basename-rmext.java.html
Recommended Posts