130. Converting file paths

Converting a file path into a String, URI, File, and so on is a common task that can occur in a wide range of applications. Let's consider the following file path:

Path path = Paths.get("/learning/packt", "JavaModernChallenge.pdf");

Now, based on JDK 7 and the NIO.2 API, let's see how we can convert a Path into a String, a URI, an absolute path, a real path, and a file:

  • Converting a Path into a String is as simple as calling (explicitly or automatically) the Path.toString() method. Notice that if the path was obtained via the FileSystem.getPath() method, then the path-string returned by toString() may differ from the initial String that was used to create the path:
// learningpacktJavaModernChallenge.pdf
String pathToString = path.toString();
  • Converting a Path into a URI (browser format) can be accomplished via the Path.toURI() method. The returned URI wraps a path-string that can be used in the address bar of a web browser:
// file:///D:/learning/packt/JavaModernChallenge.pdf
URI pathToURI = path.toUri();

Let's say that we want to extract the filename present in a URI/URL as Path (this is a common scenario to encounter). In such cases, we can rely on the following snippets of code:

// JavaModernChallenge.pdf
URI uri = URI.create(
"https://www.learning.com/packt/JavaModernChallenge.pdf");
Path URIToPath = Paths.get(uri.getPath()).getFileName();

// JavaModernChallenge.pdf
URL url = new URL(
"https://www.learning.com/packt/JavaModernChallenge.pdf");
Path URLToPath = Paths.get(url.getPath()).getFileName();

Conversion of paths can be done as follows:

  • Converting a relative Path into an absolute Path can be done via the Path.toAbsolutePath() method. If the Path is already absolute, then the same result will be returned:
// D:learningpacktJavaModernChallenge.pdf
Path pathToAbsolutePath = path.toAbsolutePath();
  • Converting a Path into a real Path can be accomplished via the Path.toRealPath() method and its result is dependent on the implementation. If the file that's being pointed to doesn't exist, then this method will throw an IOException. But, as a rule of thumb, the result of calling this method is an absolute path without redundant elements (normalized). This method gets an argument that indicates how symbolic links should be treated. By default, if the filesystem supports symbolic links, then this method will try to resolve them. If you wish to ignore the symbolic links, simply pass the LinkOption.NOFOLLOW_LINKS constant to the method. Moreover, the path name elements will represent the actual name of the directories and the file.

For example, let's consider the following Path and the result of calling this method (notice that we have intentionally added several redundant elements and capitalized the PACKT folder):

Path path = Paths.get(
"/learning/books/../PACKT/./", "JavaModernChallenge.pdf");

// D:learningpacktJavaModernChallenge.pdf
Path realPath = path.toRealPath(LinkOption.NOFOLLOW_LINKS);
  • Converting a Path into a file can be done via the Path.toFile() method. For converting a file into a Path, we can rely on the File.toPath() method:
File pathToFile = path.toFile();
Path fileToPath = pathToFile.toPath();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset