简体   繁体   中英

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:

        String title = fc.getSelectedFile().toString();

But lets say I have the path "home/user/file.txt". How would I strip off the path and make it so the filename displays as "file.txt" only?

文件getName()返回所需内容,即仅是路径名名称序列中的姓氏。

getSelectedFile() returns a File object; the easiest thing to do would be to just call getName() on the File object. If the path comes from someplace else, you could actually construct a File from it and then call getName() .

String filename = title.substring(title.lastIndexOf("/"))

编辑:大脑的答案虽然更好,也更简洁:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM