Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Java file:// URL to File(...) path, platform independent, including UNC paths

Tags:

java

url

windows

I am developing a platform independent application. I am receiving a file URL*. On windows these are:

  • file:///Z:/folder%20to%20file/file.txt

  • file://host/folder%20to%20file/file.txt (an UNC path)

I am using new File(URI(urlOfDocument).getPath())which works fine with the first one and also on Unix, Linux, OS X, but does not work with UNC paths.

What is the standard way to convert file: URLs to File(..) paths, being compatible with Java 6?

......

* Note: I am receiving theses URLs from OpenOffice / LibreOffice (XModel.getURL()).

like image 369
Christian Fries Avatar asked Aug 29 '13 21:08

Christian Fries


People also ask

How do you change a file path in Java?

Convert Path to File In Java, we can use path. toFile() to convert a Path into a File .

Can a URL be a file path?

So yes, file URIs are URLs.

How do you get the path of the file in the file Java?

In Java, for NIO Path, we can use path. toAbsolutePath() to get the file path; For legacy IO File, we can use file. getAbsolutePath() to get the file path.

What does @path do in Java?

A Path can represent a root, a root and a sequence of names, or simply one or more name elements. A Path is considered to be an empty path if it consists solely of one name element that is empty. Accessing a file using an empty path is equivalent to accessing the default directory of the file system.


1 Answers

Based on the hint and link provided in Simone Giannis' answer, this is my hack to fix this.

I am testing on uri.getAuthority(), because UNC path will report an Authority. This is a bug - so I rely on the existence of a bug, which is evil, but it apears as if this will stay forever (since Java 7 solves the problem in java.nio.Paths).

Note: In my context I will receive absolute paths. I have tested this on Windows and OS X.

(Still looking for a better way to do it)

package com.christianfries.test;  import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL;  public class UNCPathTest {      public static void main(String[] args) throws MalformedURLException, URISyntaxException {         UNCPathTest upt = new UNCPathTest();                  upt.testURL("file://server/dir/file.txt");  // Windows UNC Path          upt.testURL("file:///Z:/dir/file.txt");     // Windows drive letter path                  upt.testURL("file:///dir/file.txt");        // Unix (absolute) path     }      private void testURL(String urlString) throws MalformedURLException, URISyntaxException {         URL url = new URL(urlString);         System.out.println("URL is: " + url.toString());          URI uri = url.toURI();         System.out.println("URI is: " + uri.toString());                  if(uri.getAuthority() != null && uri.getAuthority().length() > 0) {             // Hack for UNC Path             uri = (new URL("file://" + urlString.substring("file:".length()))).toURI();         }          File file = new File(uri);         System.out.println("File is: " + file.toString());          String parent = file.getParent();         System.out.println("Parent is: " + parent);          System.out.println("____________________________________________________________");     }  } 
like image 120
Christian Fries Avatar answered Sep 19 '22 18:09

Christian Fries