仅获取路径目录并丢弃Java中的文件 [英] Get path directory only and discard file in Java
本文介绍了仅获取路径目录并丢弃Java中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们如何实际上丢弃Java字符串中的最后一个文件并仅获取文件路径目录?
How do we actually discard last file from java string and just get the file path directory?
用户输入的随机路径:
C:/my folder/tree/apple.exe
所需的输出:
C:/my folder/tree/
我发现的
最近的解决方案来自此处 .该论坛的答案仅显示最后获取的字符串,而不显示其余字符串.我想显示字符串的其余部分.
closest solution i found is from here . The answer from this forum only display last string acquired not the rest of it. I want to display the rest of the string.
推荐答案
最简单,最具故障保护性(跨平台)的解决方案是从路径创建File
对象.
The easiest and most failsafe (read: cross-platform) solution is to create a File
object from the path.
赞:
File myFile = new File( "C:/my folder/tree/apple.exe" );
// Now get the path
String myDir = myFile.getParent();
这篇关于仅获取路径目录并丢弃Java中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文