12

Below is a path to my Windows directory. Normally the path should have \ instead of // but both seem to work.

String WinDir = "C://trash//blah//blah";

Same for a Linux path. The normal should have a / instead of //. The below and above snippet work fine and will grab the contents of the files specified.

String LinuxDir = "//foo//bar//blah"

So, both use strange declarations of file paths, but both seem to work fine. Elaboration please.

For example,

 File file = new File(WinDir);`
 file.mkdir();`
Nicolás Alarcón Rapela
  • 2,714
  • 1
  • 18
  • 29
Mason T.
  • 1,567
  • 1
  • 14
  • 33

2 Answers2

26

Normally, when specifying file paths on Windows, you would use backslashes. However, in Java, and many other places outside the Windows world, backslashes are the escape character, so you have to double them up. In Java, Windows paths often look like this: String WinDir = "C:\\trash\\blah\\blah";. Forward slashes, on the other hand, do not need to be doubled up and work on both Windows and Unix. There is no harm in having double forward slashes. They do nothing to the path and just take up space (// is equivalent to /./). It looks like someone just did a relpace of all backslashes into forward slashes. You can remove them. In Java, there is a field called File.separator (a String) and File.separatorChar (a char), that provide you with the correct separator (/ or \), depending on your platform. It may be better to use that in some cases: String WinDir = "C:" + File.separator + "trash" + File.separator + "blah" + File.separator + "blah";

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • 2
    @SotiriosDelimanolis. Sure. `//` is eqivalent to `/./`. It's a perfectly valid construct. Just takes up space in the string. – Mad Physicist Jan 07 '14 at 19:16
  • Thank you, that's what I was curious about the whole time. – Mason T. Jan 07 '14 at 19:17
  • Sorry, but I found this comment useless: `WinDir = "C:" + File.separator + "trash"`. If you use `C:` what is the point on getting the `File.separator`? You already know it is Windows and will always be a backslash. – user1156544 May 26 '16 at 22:51
  • 1
    @user1156544. For this particular contrived example, you are absolutely correct. However, you do not always have all the path elements hard coded and you do not always know what platform you are running on. Java is after all intended to be a cross-platform language. – Mad Physicist May 27 '16 at 07:14
  • I see the point on having a separator, but perhaps simply using "/" is more efficient. I believe it works in all OS – user1156544 May 27 '16 at 12:25
  • @user1156544. Not on older versions of Windows. Also, some windows applications refuse to accept `/` even now. – Mad Physicist May 27 '16 at 15:48
  • Thanks for the clarification. If by older you mean older than Windows XP I would not consider a problem. What do you mean by Windows applications? Within Java it will work. – user1156544 May 27 '16 at 15:55
  • @user1156544 if you are building a command line to pass to a CMD.EXE process, for example, you are compelled to use backslashes. In my experience, this is one of the very few contexts in which backslashes are required, although MadPhysicist may have in mind some others. – philwalk Sep 25 '17 at 20:12
  • Repeated slashes (and backslashes) are fine everywhere except at the start of a Windows path, for which two or more slashes indicates a UNC path, and three or more is a broken UNC path with an empty initial component (the server, domain or namespace component, e.g. "//server/share/file"). Even in POSIX, exactly two initial slashes is reserved for use by the OS (e.g. bash preserves them, as does Python's `posixpath` library and `pathlib.PosixPath`), but most Unix systems handle it as just a single slash. – Eryk Sun Jun 01 '20 at 07:20
  • 2
    If you're using the Windows API extensively, there are several functions that only accept backslash. For example, the path manipulation functions such as `PathCchStripToRoot`. And the search function `NeedCurrentDirectoryForExePathW`, which is called by `CreateProcessW` to determine whether "." should be added to its EXE search path. Also, `CreateSymbolicLinkW` sets a broken substitute path if a relative symlink uses slash instead of backslash (e.g. "/rooted/relative/symlink" or "../parent/relative/symlink"). It's a bug because the kernel only reserves backslash when evaluating the link. – Eryk Sun Jun 01 '20 at 07:36
0

With java.nio.path, you even better get an independent OS path without any concern about path delimiter.

public class PathsGetMethod {
    public static void main(String[] args) {
        Path path = Paths.get("C:\\Users\\conta\\OneDrive\\", "desktop", "data");
        System.out.println(path); 
        //C:\Users\conta\OneDrive\desktop\data
    }
}
logbasex
  • 1,688
  • 1
  • 16
  • 22