0

If user has not mounted a remote drive and is just using the \\ syntax how do I convert such a path (\\nas) held in a String to a file in Java, sorry not really sure what you call this \\ naming.

Also is this windows specific, can it also be //

Marged
  • 10,577
  • 10
  • 57
  • 99
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351

1 Answers1

2

You can pass any valid file name to the constructor of a File and Java will handle that for you. e.g.

File input = new File("\\\\nas\\somefile.txt");

will work just fine. Note the escaped backslashs. Java can handle forward slashes just as well, so the above can be written as:

File input = new File("//nas/somefile.txt");

A filename like \\nas\somefile.txt is called a UNC path

  • Does Java itself replace forward slashes with backslashes in a file path? If it instead relies on Windows to normalize the path, then note that local-device paths that begin with "\\?\" must use backslash as the path separator. Windows skips all normalization for this namespace, except to replace WinAPI "\\?\" with NTAPI "\??\". – Eryk Sun May 14 '18 at 17:29
  • Thanks, but I realize what is confusing me if that although you can create a File as described if you then call file.exists() it returns false, why would that be ? – Paul Taylor May 14 '18 at 18:53