Java File.separator 和路径中斜杠的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2417485/
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 07:20:41  来源:igfitidea点击:

Difference between File.separator and slash in paths

java

提问by Joe23

What is the difference between using File.separatorand a normal /in a Java Path-String?

Java Path-String 中的usingFile.separator和 normal/有什么区别?

In contrast to double backslash \\platform independence seems not to be the reason, since both versions work under Windows and Unix.

与双反斜杠相比,\\平台独立性似乎不是原因,因为这两个版本都可以在 Windows 和 Unix 下运行。

public class SlashTest {
    @Test
    public void slash() throws Exception {
        File file = new File("src/trials/SlashTest.java");
        assertThat(file.exists(), is(true));
    }

    @Test
    public void separator() throws Exception {
        File file = new File("src" + File.separator + "trials" + File.separator + "SlashTest.java");
        assertThat(file.exists(), is(true));
    }
}

To rephrase the question, if /works on Unix and Windows, why should one ever want to use File.separator?

重新表述这个问题,如果/在 Unix 和 Windows 上工作,为什么要使用File.separator

采纳答案by T.J. Crowder

With the Java libraries for dealing with files, you can safely use /(slash, not backslash) on all platforms. The library code handles translating things into platform-specific paths internally.

使用用于处理文件的 Java 库,您可以/在所有平台上安全地使用(斜线,而不是反斜线)。库代码在内部处理将事物转换为特定于平台的路径。

You might want to use File.separatorin UI, however, because it's best to show people what will make sense in their OS, rather than what makes sense to Java.

File.separator但是,您可能希望在 UI 中使用,因为最好向人们展示什么在他们的操作系统中有意义,而不是什么对 Java 有意义。

Update: I have not been able, in five minutes of searching, to find the "you can always use a slash" behavior documented. Now, I'm sure I've seen it documented, but in the absense of finding an official reference (because my memory isn't perfect), I'd stick with using File.separatorbecause you knowthat will work.

更新:在五分钟的搜索中,我无法找到记录的“您始终可以使用斜杠”行为。现在,我确定我已经看到它被记录在案,但在没有找到官方参考资料的情况下(因为我的记忆力并不完美),我会坚持使用,File.separator因为你知道这会起作用。

回答by jpabluz

Well, there are more OS's than Unix and Windows (Portable devices, etc), and Java is known for its portability. The best practice is to use it, so the JVM could determine which one is the best for that OS.

嗯,有比 Unix 和 Windows(便携式设备等)更多的操作系统,Java 以其可移植性而闻名。最佳实践是使用它,以便 JVM 可以确定哪个最适合该操作系统。

回答by Pointy

You use File.separatorbecause someday your program might run on a platform developed in a far-off land, a land of strange things and stranger people, where horses cry and cows operate all the elevators. In this land, people have traditionally used the ":" character as a file separator, and so dutifully the JVM obeys their wishes.

你使用File.separator是因为有一天你的程序可能会运行在一个开发于遥远土地上的平台上,一个陌生的地方和陌生的人,在那里马在哭,牛在运行所有的电梯。在这片土地上,人们传统上使用“:”字符作为文件分隔符,因此 JVM 尽职尽责地遵守他们的意愿。

回答by Holograham

portability plain and simple.

便携性简单明了。

回答by Yishai

Although using File.separator to reference a file name is overkill (for those who imagine far off lands, I imagine their JVM implementation would replace a /with a :just like the windows jvm replaces it with a \).

尽管使用 File.separator 来引用文件名是多余的(对于那些想象遥远的地方的人,我想他们的 JVM 实现会将 a 替换为/a:就像 windows jvm 将其替换为 a 一样\)。

However, sometimes you are getting the file reference, not creating it, and you need to parse it, and to be able to do that, you need to know the separator on the platform. File.separator helps you do that.

但是,有时您是获取文件引用,而不是创建它,您需要解析它,为了能够做到这一点,您需要知道平台上的分隔符。File.separator 可以帮助您做到这一点。

回答by William Billingsley

Although it doesn't make much difference on the way in, it does on the way back.

虽然它在进入的路上没有太大的区别,但在回来的路上却有很大的不同。

Sure you can use either '/' or '\' in new File(String path), but File.getPath() will only give you one of them.

当然你可以在 new File(String path) 中使用 '/' 或 '\',但 File.getPath() 只会给你其中之一。

回答by shubhankar

The pathname for a file or directory is specified using the naming conventions of the host system. However, the File class defines platform-dependent constants that can be used to handle file and directory names in a platform-independent way.

文件或目录的路径名是使用主机系统的命名约定指定的。但是,File 类定义了平台相关的常量,这些常量可用于以独立于平台的方式处理文件和目录名称。

Files.seperator defines the character or string that separates the directory and the file com- ponents in a pathname. This separator is '/', '\' or ':' for Unix, Windows, and Macintosh, respectively.

Files.seperator 定义在路径名中分隔目录和文件组件的字符或字符串。对于 Unix、Windows 和 Macintosh,此分隔符分别是“/”、“\”或“:”。

回答by ceilfors

If you are using Java 7, checkout Path.resolve()and Paths.get().

如果您使用的是 Java 7,请查看 Path.resolve()Paths.get()

回答by Erik Bennett

"Java SE8 for Programmers" claims that the Javawill cope with either. (pp. 480, last paragraph). The example claims that:

“Java SE8 for Programmers”声称Java可以处理其中任何一个。(第 480 页,最后一段)。该示例声称:

c:\Program Files\Java\jdk1.6.0_11\demo/jfc

will parse just fine. Take note of the last (Unix-style) separator.

会解析得很好。注意最后一个(Unix 风格的)分隔符。

It's tacky, and probably error-prone, but it is what they (Deitel and Deitel) claim.

这很俗气,而且可能容易出错,但这正是他们(Deitel 和 Deitel)所声称的。

I think the confusion for people, rather than Java, is reason enough not to use this (mis?)feature.

我认为人们的困惑,而不是 Java,足以成为不使用这个(错误?)特性的理由。

回答by E_X

As the gentlemen described the difference with variant details.

正如先生们所描述的那样,差异细节。

I would like to recommend the use of the Apache Commons ioapi, class FilenameUtilswhen dealing with files in a program with the possibility of deploying on multiple OSs.

我想推荐在处理程序中的文件时使用Apache Commons ioapi 类,该类FilenameUtils可以部署在多个操作系统上。