Java 正斜杠还是反斜杠?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19762169/
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
Forward slash or backslash?
提问by Patriot524
I am looking to write and read text files to and from (respectively) a directory different from that of my program. When I specify a directory to write to or read from, should I be using forward slashes or backslashes to identify a file path?
我希望在(分别)与我的程序目录不同的目录中写入和读取文本文件。当我指定要写入或读取的目录时,我应该使用正斜杠还是反斜杠来标识文件路径?
回答by Little Child
You could use either.
你可以使用。
If you use /
then you only need a single slash.
If you use \
, you need to use \\
. That is, you need to escape it.
如果您使用,/
那么您只需要一个斜杠。
如果使用\
,则需要使用\\
. 也就是说,你需要逃避它。
You can also use the resolve()
method of the java.nio.Path
class to add directories / files to the existing path. That avoids the hassle of using forward or backward slashes. You can then get the absolute path by calling the toAbsolutePath()
method followed by toString()
您还可以使用类的resolve()
方法java.nio.Path
将目录/文件添加到现有路径。这避免了使用正斜杠或反斜杠的麻烦。然后您可以通过调用toAbsolutePath()
方法获取绝对路径,然后toString()
SSCCE:
SSCCE:
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathSeperator {
public static void main(String[] args) {
// the path seperator for this system
String pathSep = System.getProperty("path.separator");
// my home directory
Path homeDir = Paths.get(System.getProperty("user.home"));
// lets print them
System.out.println("Path Sep: " + pathSep);
System.out.println(homeDir.toAbsolutePath());
// as it turns out, on my linux it is a colon
// and Java is using forward slash internally
// lets add some more directories to the user.home
homeDir = homeDir.resolve("eclipse").resolve("configuration");
System.out.println("Appending more directories using resolve()");
System.out.println(homeDir);
}
}
回答by T.J. Crowder
I've never found it documentedanywhere, but the JDK classes let you use slashes regardless of whether you're on Windows or not. (You can see this in the JDK source, where it explicitly converts path separators for you.)
我从未在任何地方找到它的文档,但是无论您是否在 Windows 上,JDK 类都允许您使用斜杠。(您可以在 JDK 源代码中看到这一点,它为您显式转换了路径分隔符。)
Officially — and certainly in any UI you're doing — you should use the file.separator
system property, which is available via System.getProperty
(the list of standard system properties is documented in the docs for System.getProperties
):
正式地——当然在你正在做的任何 UI 中——你应该使用file.separator
系统属性,它可以通过以下方式获得System.getProperty
(标准系统属性列表记录在文档中System.getProperties
):
String sep = System.getProperty("file.separator");
...and also via the static
fields They're also available as File.separator
(and File.separatorChar
).
...以及通过static
字段它们也可以作为File.separator
(和File.separatorChar
)使用。
You can also use the various features of the java.io.File
classfor combining and splitting paths, and/or the various features of the interfaces and classes in java.nio.file
.
您还可以使用的各种功能的java.io.File
类进行组合和分离路径,和/或在接口和类的各种功能java.nio.file
。
回答by Paul Draper
Using forward slashes will make it system independent. I'd stick to that for simplicity.
使用正斜杠将使其独立于系统。为简单起见,我会坚持这一点。
Consider using java.io.File.separator
if you ever display the path to the user. You'd rather not surprise those Windows users. They're a jumpy lot.
java.io.File.separator
如果您曾经向用户显示路径,请考虑使用。您不想让那些 Windows 用户感到惊讶。他们是一群神经质的人。
回答by Ryan
You should use /
你应该使用 /
For example C:/User/...
例如 C:/User/...