Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward slash or backslash?

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?

like image 384
Patriot524 Avatar asked Nov 04 '13 05:11

Patriot524


People also ask

What is the difference between backslash and forward slash?

Summary: The Backslash and Forward SlashThe backslash (\) is mostly used in computing and isn't a punctuation mark. The forward slash (/) can be used in place of “or” in less formal writing. It's also used to write dates, fractions, abbreviations, and URLs.

When would you use a forward slash?

The Forward Slash Perhaps its most common use is to mean 'or' when presenting two alternatives: Each speaker will give a presentation on a topic of his/her choice. The slash here shows that either word could apply. However, you should avoid doing this too often in formal writing, where 'or' is a better choice.

Does Windows use forward slash or backslash?

Windows uses backslashes for paths, while everything else seems to use forward slashes. Modern software tries to automatically correct you when you type the wrong type of slash, so it doesn't matter which type of slash you use most of the time.

When did backslash become forward slash?

MS-DOS 2.0, released 1983, copied the hierarchical file system from Unix and thus used the (forward) slash as the directory separator.


3 Answers

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.

like image 99
Paul Draper Avatar answered Oct 25 '22 03:10

Paul Draper


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.

like image 24
T.J. Crowder Avatar answered Oct 25 '22 02:10

T.J. Crowder


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

    }
}  
like image 34
An SO User Avatar answered Oct 25 '22 02:10

An SO User