[英]Take off file path (home/user/file.txt = file.txt)?
So I've written a simple text editor in java, and it retrieves the file via showOpenDialog()
and converts the filename into a string, so it can be displayed as the title: 因此,我用Java语言编写了一个简单的文本编辑器,它通过showOpenDialog()
检索文件并将文件名转换为字符串,因此可以将其显示为标题:
String title = fc.getSelectedFile().toString();
But lets say I have the path "home/user/file.txt". 但是,可以说我有路径“ home / user / file.txt”。 How would I strip off the path and make it so the filename displays as "file.txt" only? 我将如何删除路径并使之命名,以便文件名仅显示为“ file.txt”?
文件getName()返回所需内容,即仅是路径名名称序列中的姓氏。
getSelectedFile()
returns a File
object; getSelectedFile()
返回一个File
对象; the easiest thing to do would be to just call getName()
on the File
object. 最简单的getName()
是在File
对象上调用getName()
。 If the path comes from someplace else, you could actually construct a File
from it and then call getName()
. 如果路径来自其他地方,则实际上可以从中构造一个File
, 然后调用getName()
。
String filename = title.substring(title.lastIndexOf("/"))
编辑:大脑的答案虽然更好,也更简洁:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.