Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Path conversion

farenda 2016-04-18 0

Java Path conversion

In this tutorial we’re going to show how to convert between Path, legacy File, and URI. Path conversion allows to effectively work with legacy libraries.

Sometimes, when working with legacy libraries, there is a need to convert from java.io.File to java.nio.file.Path or other way around. Also, sometimes, there’s a path in URI format, which also can be transformed into Java 7 Path.

Path to/from File conversion:

Conversion from/to File using File.toPath() and Path.toFile() methods:

Path path = new File("/proc/version").toPath();
System.out.println("Path from File: " + path);

System.out.println("path.toFile(): " + path.toFile());

Results:

Path from File: /proc/version
path.toFile(): /proc/version

Path to/from URI conversion:

Conversion from/to URI using Paths.get(URI) and Path.toURI():

Path path = Paths.get("/proc/version");
System.out.println("path.toUri(): " + path.toUri());

Path path = Paths.get(URI.create("file:///proc/version"));
System.out.println("Path from URI: " + path);

Results:

path.toUri(): file:///proc/version
Path from URI: /proc/version

API:

  • java.io.File
  • java.net.URI
  • java.nio.file.Path
  • java.nio.file.Paths

Check out Java IO Tutorial to learn more about Java NIO!

References

  • Oracle Java 8 API
Share with the World!
Categories Java Tags java, java-io
Previous: Java Path create
Next: Java Path symlink

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored