java 7 NIO2(1) Path類相關新API
阿新 • • 發佈:2019-02-20
java 7 新的NIO API新增了FileSystem和FileStore相關API,最主要的是Path.java類,主要是我們常見的檔案的一些相關操作,這個Path是NIO IO操作的基礎,我們可以定義一個檔案路徑,對路徑的相關操作等,
我們先看下java7 NIO 2新增的API:
我們看下這個API使用的示例:
import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.Paths; //NIO 2 file APi filesystem filestore public class NIO2File { public static void main(String[] args) { try { // FileSystems.getDefault().getPath(first, more) Path path = Paths.get(System.getProperty("user.home"), "www", "pyweb.settings"); Path real_path = path.toRealPath(LinkOption.NOFOLLOW_LINKS); System.out.println("Path to real path: " + real_path); System.out.println("Number of name elements in path: " + path.getNameCount()); for (int i = 0; i < path.getNameCount(); i++) { System.out.println("Name element " + i + " is: " + path.getName(i)); } System.out.println("Subpath (0,3): " + path.subpath(0, 3)); 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()); Path base = Paths.get(System.getProperty("user.home"), "www", "pyweb.settings"); // resolve AEGON.txt file Path path1 = base.resolve("django.wsgi"); System.out.println(path1.toString()); Path path2 = base.resolveSibling(".bashrc"); System.out.println(path2.toString()); Path path01_to_path02 = path1.relativize(path2); System.out.println(path01_to_path02); try { boolean check = Files.isSameFile(path1.getParent(), path2.getParent()); if (check) { System.out.println("The paths locate the same file!"); // true } else { System.out .println("The paths does not locate the same file!"); } } catch (IOException e) { System.out.println(e.getMessage()); } boolean sw = path1.startsWith("/rafaelnadal/tournaments"); boolean ew = path1.endsWith("django.wsgi"); System.out.println(sw); System.out.println(ew); for (Path name : path1) { System.out.println(name); } } catch (NoSuchFileException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } } }
輸出:
Path to real path: /home/weijianzhongwj/www/pyweb.settings Number of name elements in path: 4 Name element 0 is: home Name element 1 is: weijianzhongwj Name element 2 is: www Name element 3 is: pyweb.settings Subpath (0,3): home/weijianzhongwj/www Path to file name: pyweb.settings File to path: /home/weijianzhongwj/www/pyweb.settings /home/weijianzhongwj/www/pyweb.settings/django.wsgi /home/weijianzhongwj/www/.bashrc ../../.bashrc The paths does not locate the same file! false true home weijianzhongwj www pyweb.settings django.wsgi
API很類似linux上的檔案操作命令,新增的unix fifilesystem等。