javawindowsjava-ionio2

How do i convert paths that contain unc shares


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 //


Solution

  • 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