文件路径 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:06:00  来源:igfitidea点击:

file path Windows format to java format

java

提问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

String path = "C:\Documents and Settings\Manoj\Desktop";
path = path.replace("\", "/");
// or
path = path.replaceAll("\\", "/");

Find more details in the Docs

文档中查找更多详细信息

回答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

Strings are immutable. Once they are created, you can't change them. This means replacereturns a new String where the target("\\") is replaced by the replacement("/"). Simply calling replacewill not change path.

Strings 是不可变的。一旦它们被创建,你就不能改变它们。这意味着replace返回一个新的字符串,其中目标("\\")被替换("/")替换。简单地调用replace不会改变path

The difference between replaceAlland replaceis that replaceAll will search for a regex, replace doesn't.

replaceAll和之间的区别在于replacereplaceAll 将搜索正则表达式,而 replace 不会。

回答by dionoid

Java 7 and up supports the Pathclass (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

谢谢