Convert File to Path and Path to File
Overview
In this article, we will learn how to convert a File to a Path in Java.
Convert File to Path
package com.geekscoder.io;
import java.io.File;
import java.nio.file.Path;
public class FileToPath {
public static void main(String[] args) {
File file = new File("/home/geekscoder/file.txt");
Path path = file.toPath(); // Java 1.7, convert File to Path
System.out.println(path);
}
}
Convert Path to File
package com.geekscoder.io;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathToFile {
public static void main(String[] args) {
Path path = Paths.get("/home/geekscoder/file.txt");
File file = path.toFile(); // Convert Path to File
System.out.println(file);
}
}