如何从包含绝对文件路径的字符串中获取文件名? [英] How do I get the file name from a String containing the Absolute file path?
本文介绍了如何从包含绝对文件路径的字符串中获取文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
String
变量包含一个文件名, C:\Hello\AnotherFolder\The File Name.PDF
。如何仅将文件名文件名.PDF
作为字符串?
String
variable contains a file name, C:\Hello\AnotherFolder\The File Name.PDF
. How do I only get the file name The File Name.PDF
as a String?
我打算拆分字符串,但这不是最优解。
I planned to split the string, but that is not the optimal solution.
推荐答案
只需使用 File.getName()
just use 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));
这篇关于如何从包含绝对文件路径的字符串中获取文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文