In this tutorial , we will see how we can convert file to path using Java ? The java.nio.file package includes Java virtual machine interfaces and classes for accessing files, file attributes, and file systems.

Convert File to Path

If we want to convert from java.io.File to java.nio.file.Path we can use toPath()


// Convert File to Path
File file = new File("/home/mkyong/test/file.txt");
Path path = file.toPath();

Convert Path to File

If we want to convert from java.nio.file.Path to java.io.File we can use toFile()


// Convert Path to File
Path path = Paths.get("/home/mkyong/test/file.txt");
File file = path.toFile();

Also read , how to convert List to int[] in Java

References

Laisser un commentaire