如何从包含绝对文件路径的字符串中获取文件名?
字符串
变量包含一个文件名,c:\ Hello \ elesherfolder \ file name.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
只需使用 file。 getName()
使用字符串方法:
just use File.getName()
using String methods:
使用 path (java 7+):
请注意,
\\
将字符串分配为平台依赖性,因为文件分离器可能会有所不同。路径#getName
为您解决这个问题。Alternative using
Path
(Java 7+):Note that splitting the string on
\\
is platform dependent as the file separator might vary.Path#getName
takes care of that issue for you.使用
filenameutils
in apache commons io :Using
FilenameUtils
in Apache Commons IO :任何文件名/路径操作都应在
java.nio.file
package 。具体来说, <代码>路径 class
您可以使用
paths
工厂方法,get
(或所述filesystems
类,如上所述在这里)。一旦拥有适当的
路径>代表完整路径的实例,您就可以使用
getfilename
例如,
注意:所有这些都假定您在Windows环境中运行(考虑您的文件路径)。如果没有,您需要使用
filesystemprovider
支持Windows路径命名。另外,只有字符串操作,并且考虑
字符串
您要问的是我们需要在最后一个分隔器之后提取所有内容,即。
\
。 感兴趣的。就是我们
那 /代码>。
如果您有一个带有不同分离器的字符串,请调整
lastIndexof
使用该分隔符。(甚至还有 Overload 接受整个<代码>字符串作为分隔符。)我在上面的示例中省略了它,但是如果您不确定
String
来自何处或可能包含的位置,您将需要要验证lastIndexof
返回非负值,因为 javadoc states 它将返回Any file name/path manipulation should go through APIs in the
java.nio.file
package.Specifically, the
Path
classYou can get an instance of type
Path
using thePaths
factory method,get
(or theFileSystems
class as described here).Once you have an appropriate
Path
instance representing your full path, you can usegetFileName
For example,
Note: This all assumes you're running in a Windows environment (considering your file path). If not, you'll need to use a
FileSystemProvider
that supports Windows path naming.Alternatively, with only string manipulation, and considering the
String
you're asking about iswe need to extract everything after the last separator, ie.
\
. That is what we are interested in.You can do
This will retrieve the index of the last
\
in yourString
and extract everything that comes after it intofileName
.If you have a
String
with a different separator, adjust thelastIndexOf
to use that separator. (There's even an overload that accepts an entireString
as a separator.)I've omitted it in the example above, but if you're unsure where the
String
comes from or what it might contain, you'll want to validate that thelastIndexOf
returns a non-negative value because the Javadoc states it'll return自1.7以来
Since 1.7
其他答案对我的特定情况并不完全有用,在该方案中,我正在阅读与当前的操作系统不同的源自操作系统的路径。为了详细说明,我要保存从Linux服务器上的Windows平台保存的电子邮件附件。从Javamail API返回的文件名类似于“ C:\ temp \ Hello.xls”
我最终得到的解决方案:
The other answers didn't quite work for my specific scenario, where I am reading paths that have originated from an OS different to my current one. To elaborate I am saving email attachments saved from a Windows platform on a Linux server. The filename returned from the JavaMail API is something like 'C:\temp\hello.xls'
The solution I ended up with:
您可以使用路径= c:\ hello \ enosefolder \ thefilename.pdf
you can use path = C:\Hello\AnotherFolder\TheFileName.PDF
java.nio.file.path的getFilename()方法用于返回该路径对象指向的文件或目录的名称。
路径getFilename()
参考的
: https:https:https:// wwww。 geeksforgeeks.org/path-getfilename-method-in-java-with-examples/
getFileName() method of java.nio.file.Path used to return the name of the file or directory pointed by this path object.
Path getFileName()
For reference:
https://www.geeksforgeeks.org/path-getfilename-method-in-java-with-examples/
认为Java是乘数:
Considere the case that Java is Multiplatform:
一种无依赖性的方法,并照顾 .. ,。和重复的分离器。
测试用例:
也许GetFilename有点令人困惑,因为它也返回目录名称。它在路径中返回文件的名称或最后一个目录。
A method without any dependency and takes care of .. , . and duplicate separators.
Test case:
Maybe getFileName is a bit confusing, because it returns directory names also. It returns the name of file or last directory in a path.
使用Java Regex *提取文件名 *。
extract file name using java regex *.
您可以使用FileInfo对象获取文件的所有信息。
You can use FileInfo object to get all information of your file.