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:

path1
path1

 

posted @ 2016-01-06 13:53  springup  阅读(227)  评论(0)    收藏  举报