20

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Patriot524
  • 201
  • 1
  • 3
  • 7

4 Answers4

39

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.

Paul Draper
  • 78,542
  • 46
  • 206
  • 285
18

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @LittleChild: Thanks, added a note about `java.nio.file`. – T.J. Crowder Nov 04 '13 at 05:39
  • I believe you might have meant to refer to `System.getProperty("file.separator")` (which returns a `/` on UNIX), instead of `System.getProperty("path.separator")` (which returns a `;` on UNIX) – Luke May 09 '14 at 22:28
5

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

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

    }
}  
An SO User
  • 24,612
  • 35
  • 133
  • 221
0

You should use /

For example C:/User/...

Ryan
  • 231
  • 3
  • 9
  • 13
    Your answer would be dramatically improved if you answered *why*? – Michael Petrotta Nov 04 '13 at 05:56
  • Backslash is sometimes needed, but for publicly shared code is better avoided, due to portability considerations. It has only been rarely useful or necessary in my experience, even when developing on Windows. – philwalk Sep 12 '17 at 21:33