文件路径 Windows 格式转 java 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3059383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
file path Windows format to java format
提问by Manoj
I need to convert the file path in windows say C:\Documents and Settings\Manoj\Desktop for java as C:/Documents and Settings/Manoj/Desktop .
我需要将 Windows 中的文件路径转换为 C:\Documents and Settings\Manoj\Desktop for java 为 C:/Documents and Settings/Manoj/Desktop 。
Is there any utility to convert like this.?
是否有任何实用程序可以像这样转换。?
采纳答案by Fred
回答by Martijn Courteaux
String path = "C:\Documents and Settings\Manoj\Desktop";
String javaPath = path.replace("\", "/"); // Create a new variable
or
或者
path = path.replace("\", "/"); // Just use the existing variable
String
s are immutable. Once they are created, you can't change them. This means replace
returns a new String where the target("\\"
) is replaced by the replacement("/"
). Simply calling replace
will not change path
.
String
s 是不可变的。一旦它们被创建,你就不能改变它们。这意味着replace
返回一个新的字符串,其中目标("\\"
)被替换("/"
)替换。简单地调用replace
不会改变path
。
The difference between replaceAll
and replace
is that replaceAll will search for a regex, replace doesn't.
replaceAll
和之间的区别在于replace
replaceAll 将搜索正则表达式,而 replace 不会。
回答by dionoid
Java 7 and up supports the Path
class (in java.nio package).
You can use this class to convert a string-path to one that works for your current OS.
Java 7 及更高版本支持Path
该类(在 java.nio 包中)。您可以使用此类将字符串路径转换为适用于您当前操作系统的路径。
Using:
使用:
Paths.get("\folder\subfolder").toString()
on a Unix machine, will give you /folder/subfolder
. Also works the other way around.
在 Unix 机器上,会给你/folder/subfolder
. 也可以反过来工作。
https://docs.oracle.com/javase/tutorial/essential/io/pathOps.html
https://docs.oracle.com/javase/tutorial/essential/io/pathOps.html
回答by Young Lee
String path = "C:\Documents and Settings\someDir";
path = path.replaceAll("\\", "/");
In Windows you should use four backslash but not two.
在 Windows 中,您应该使用四个反斜杠而不是两个。
回答by SivaPrakash Testing
its very simple,
它非常简单,
just check
只是检查
in MacOS
在 MacOS 中
File directory = new File("/Users/sivo03/eclipse-workspace/For4DC/AutomationReportBackup/"+dir);
File directoryApache = new File("/Users/sivo03/Automation/apache-tomcat-9.0.22/webapps/AutomationReport/"+dir);
and same we use in windows
和我们在 windows 中使用的一样
File directory = new File("C:\Program Files (x86)\Jenkins\workspace\BrokenLinkCheckerALL\AutomationReportBackup\"+dir);
File directoryApache = new File("C:\Users\Admin\Downloads\Automation\apache-tomcat-9.0.26\webapps\AutomationReports\"+dir);
use double backslash instead of single frontslash
使用双反斜杠代替单正斜杠
so no need any converter tool just use find and replace
所以不需要任何转换器工具只需使用查找和替换
"C:\Documents and Settings\Manoj\Desktop"to "C:\\Documents and Settings\\Manoj\\Desktop"
“C:\Documents and Settings\Manoj\Desktop”到“C:\\Documents and Settings\\Manoj\\Desktop”
Thats it
就是这样
(Hope its useful one)
(希望有用)
Thank you
谢谢