Difference between File.separator and slash in paths

后端 未结 14 1240
执笔经年
执笔经年 2020-11-22 13:15

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

In contrast to double backslash \\\\

14条回答
  •  感情败类
    2020-11-22 13:37

    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.

    You might want to use File.separator in UI, however, because it's best to show people what will make sense in their OS, rather than what makes sense to 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.separator because you know that will work.

提交回复
热议问题