你的位置:首页 > Java教程

[Java教程]2.2.5 NIO.2 Path 和 Java 已有的 File 类


NIO与IO交互
toPath()File -- Path
toFile()Path -- File

Demo:

import java.io.File;import java.nio.file.Path;import java.nio.file.Paths;public class PathAndFile {    public static void main(String[] args) {        File file = new File("/Users/jinxing/Documents/pathtest/path1");    Path path = Paths.get("/Users/jinxing/Documents/pathtest/path1");        File pathFile = path.toFile();    Path filePath = file.toPath();        System.out.println(pathFile.getName());    System.out.println(filePath.getFileName());      }}

Ran As Java Application:

path1path1