File類如何獲取文件後綴名
public class Demo {
public static void main(String[] args) {
File file = new File("G:\\io\\a.txt");
//獲取文件名
String name = file.getName();
//字符串截取(獲取.處的索引)然後從該索引截取
String substring = name.substring(name.indexOf("."));
//輸出
System.out.println(substring);
}
}
結果: .txt
public class Demo {
public static void main(String[] args) {
File file = new File("G:\\io\\a.txt");
//獲取文件名
String name = file.getName();
//字符串截取(獲取.處的索引)然後從該索引截取
String substring = name.substring(name.indexOf(".") + 1);
//輸出
System.out.println(substring);
}
}
結果: txt