也可以使用toFile()方法将Path转换为File对象。
import java.io.File; import java.nio.file.Path; /** nowjava - 时 代 Java 提供 **/ import java.nio.file.Paths; public class Main { public static void main(String[] args) { Path path = Paths.get("/folder1/folder2/folder4", "test.txt"); // convert path to File object File path_to_file = path.toFile(); Path file_to_path = path_to_file.toPath(); System.out.println("Path to file name: " + path_to_file.getName()); System.out.println("File to path: " + file_to_path.toString()); } }