Skip to Content
Java Coding Problems
book

Java Coding Problems

by Anghel Leonard
September 2019
Intermediate to advanced
816 pages
18h 47m
English
Packt Publishing
Content preview from Java Coding Problems

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:
// \learning\packt\JavaModernChallenge.pdfString ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Coding Problems - Second Edition

Java Coding Problems - Second Edition

Anghel Leonard
Java in a Nutshell, 7th Edition

Java in a Nutshell, 7th Edition

Benjamin J. Evans, David Flanagan
The Well-Grounded Java Developer, Second Edition

The Well-Grounded Java Developer, Second Edition

Benjamin Evans, Martijn Verburg, Jason Clark

Publisher Resources

ISBN: 9781789801415Supplemental Content