内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
String
变量包含文件名,C:\Hello\AnotherFolder\The File Name.PDF
.如何才能获得文件名?
File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF"); System.out.println(f.getName());
使用字符串方法:
File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF"); System.out.println(f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf("\\")+1));