String
变量包含文件名C:\Hello\AnotherFolder\The File Name.PDF
。如何仅将文件名The File Name.PDF
作为字符串获取?
我计划拆分字符串,但这不是最佳解决方案。
只需使用File.getName()
File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF");
System.out.println(f.getName());
使用String方法:
File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF");
System.out.println(f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf("\\")+1));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句