File類如何獲取文件後綴名

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章