File Names for Windows and Linux

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

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

The same goes for the Linux path. Normal should have / instead of //. The snippet below and above works fine and will capture the contents of the specified files.

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

So, both use weird file path declarations, but both work fine. Development, please.

For instance,

  File file = new File(WinDir);' file.mkdir();' 
+9
source share
2 answers

Typically, when specifying file paths in Windows, you use a backslash. However, in Java and in many other places outside the Windows world, backslashes are an escape character, so you need to double them. In Java, Windows paths often look like this: String WinDir = "C:\\trash\\blah\\blah"; . On the other hand, slashes do not need to be doubled and work with both Windows and Unix. There is no harm in using a double slash. They do nothing along the way and just take up space ( // equivalent to /./ ). It looks like someone just put all the backslashes on the slashes. You can delete them. In Java, there is a field called File.separator (string) and File.separatorChar (a char) that provide you with the correct delimiter ( / or \ ), depending on your platform. It may be better to use in some cases: String WinDir = "C:" + File.separator + "trash" + File.separator + "blah" + File.separator + "blah";

+18
source

The double slash "//" in your line makes a link to an empty directory. Therefore it says / "emptydirectory" / directory.

-5
source

All Articles