Java I/O How to - Connect java.io.File to java.nio.file.Path








Question

We would like to know how to connect java.io.File to java.nio.file.Path.

Answer

/*from w  w w  .  j av  a2 s  .com*/
import java.io.File;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {

  public static void main(String[] args) throws Exception {
    Path path = Paths.get(new URI("file:///C:/home/docs/users.txt"));
    File file = new File("C:\\home\\docs\\users.txt");
    Path toPath = file.toPath();
    System.out.println(toPath.equals(path));
  }
}

The code above generates the following result.