How to retrieve the UNC path instead of mapped drive path from JFileChooser

tasoocoo picture tasoocoo · Apr 19, 2011 · Viewed 8.5k times · Source

Just wondering if there is a way to return back the UNC path from a file chosen with JFileChooser. The file that I would be selecting would reside on a mapped drive that has a UNC path. Right now, I can only seem to pull back the drive letter of a mapped drive.

Answer

CRABOLO picture CRABOLO · Jul 5, 2014

From https://stackoverflow.com/users/715934/tasoocoo

I ended up finding a solution that executes the NET USE command:

 filePath = fc.getSelectedFile().getAbsolutePath();
 Runtime runTime = Runtime.getRuntime();
 Process process = runTime.exec("net use");
 InputStream inStream = process.getInputStream();
 InputStreamReader inputStreamReader = new InputStreamReader(inStream);
 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
 String line = null;
 String[] components = null;
 while (null != (line = bufferedReader.readLine())) {
   components = line.split("\\s+");
    if ((components.length > 2) && (components[1].equals(filePath.substring(0, 2)))) {
      filePath = filePath.replace(components[1], components[2]);
    }
 }