Forward slash or backslash?

前端 未结 4 2110
谎友^
谎友^ 2020-11-27 20:55

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, shou

4条回答
  •  一生所求
    2020-11-27 21:54

    I've never found it documented anywhere, 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.)

    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):

    String sep = System.getProperty("file.separator");
    

    ...and also via the static fields They're also available as File.separator (and File.separatorChar).

    You can also use the various features of the java.io.File class for combining and splitting paths, and/or the various features of the interfaces and classes in java.nio.file.

提交回复
热议问题