从Java中的文件位置获取文件名

时间:2022-10-25 16:04:52

I have a String that provides an absolute path to a file (including the file name). I want to get just the file's name. What is the easiest way to do this?

我有一个String,它提供文件的绝对路径(包括文件名)。我想得到文件的名称。最简单的方法是什么?

It needs to be as general as possible as I cannot know in advance what the URL will be. I can't simply create a URL object and use getFile() - all though that would have been ideal if it was possible - as it's not necessarily an http:// prefix it could be c:/ or something similar.

它需要尽可能通用,因为我事先无法知道URL是什么。我不能简单地创建一个URL对象并使用getFile() - 尽管这可能是理想的 - 如果它可能是 - 因为它不一定是http://前缀它可能是c:/或类似的东西。

5 个解决方案

#1


79  

new File(fileName).getName();

or

int idx = fileName.replaceAll("\\\\", "/").lastIndexOf("/");
return idx >= 0 ? fileName.substring(idx + 1) : fileName;

Notice that the first solution is system dependent. It only takes the system's path separator character into account. So if your code runs on a Unix system and receives a Windows path, it won't work. This is the case when processing file uploads being sent by Internet Explorer.

请注意,第一个解决方案取决于系统。它只考虑系统的路径分隔符。因此,如果您的代码在Unix系统上运行并且接收Windows路径,那么它将无法工作。处理Internet Explorer发送的文件上载时就是这种情况。

#2


22  

new File(absolutePath).getName();

#3


17  

Apache Commons IO provides the FilenameUtils class which gives you a pretty rich set of utility functions for easily obtaining the various components of filenames, although The java.io.File class provides the basics.

Apache Commons IO提供了FilenameUtils类,它为您提供了一组非常丰富的实用程序函数,可以轻松获取文件名的各种组件,尽管java.io.File类提供了基础知识。

#4


13  

From Apache Commons IO FileNameUtils

来自Apache Commons IO FileNameUtils

String fileName = FilenameUtils.getName(stringNameWithPath);

#5


1  

Here are 2 ways(both are OS independent.)

这里有两种方式(两者都是OS独立的。)

Using Paths : Since 1.7

使用路径:从1.7开始

Path p = Paths.get(<Absolute Path of Linux/Windows system>);
String fileName = p.getFileName().toString();
String directory = p.getParent().toString();

Using FilenameUtils in Apache Commons IO :

在Apache Commons IO中使用FilenameUtils:

String name1 = FilenameUtils.getName("/ab/cd/xyz.txt");
String name2 = FilenameUtils.getName("c:\\ab\\cd\\xyz.txt");

#1


79  

new File(fileName).getName();

or

int idx = fileName.replaceAll("\\\\", "/").lastIndexOf("/");
return idx >= 0 ? fileName.substring(idx + 1) : fileName;

Notice that the first solution is system dependent. It only takes the system's path separator character into account. So if your code runs on a Unix system and receives a Windows path, it won't work. This is the case when processing file uploads being sent by Internet Explorer.

请注意,第一个解决方案取决于系统。它只考虑系统的路径分隔符。因此,如果您的代码在Unix系统上运行并且接收Windows路径,那么它将无法工作。处理Internet Explorer发送的文件上载时就是这种情况。

#2


22  

new File(absolutePath).getName();

#3


17  

Apache Commons IO provides the FilenameUtils class which gives you a pretty rich set of utility functions for easily obtaining the various components of filenames, although The java.io.File class provides the basics.

Apache Commons IO提供了FilenameUtils类,它为您提供了一组非常丰富的实用程序函数,可以轻松获取文件名的各种组件,尽管java.io.File类提供了基础知识。

#4


13  

From Apache Commons IO FileNameUtils

来自Apache Commons IO FileNameUtils

String fileName = FilenameUtils.getName(stringNameWithPath);

#5


1  

Here are 2 ways(both are OS independent.)

这里有两种方式(两者都是OS独立的。)

Using Paths : Since 1.7

使用路径:从1.7开始

Path p = Paths.get(<Absolute Path of Linux/Windows system>);
String fileName = p.getFileName().toString();
String directory = p.getParent().toString();

Using FilenameUtils in Apache Commons IO :

在Apache Commons IO中使用FilenameUtils:

String name1 = FilenameUtils.getName("/ab/cd/xyz.txt");
String name2 = FilenameUtils.getName("c:\\ab\\cd\\xyz.txt");