File path names for Windows and Linux

后端 未结 2 1648
闹比i
闹比i 2020-12-10 15:04

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\";
         


        
2条回答
  •  旧巷少年郎
    2020-12-10 15:28

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

提交回复
热议问题