Java7中那些新特性 - 2 (文件路径篇)
Java7中对文件管理提供了大量的新API,这些新的接口可以使我们操纵文件以及文件夹更加方便。它们大多位于java.nio.file包下。
java.nio.file.Paths 包含了用于创建Path对象的静态方法java.nio.file.Path 包含了大量用于操纵文件路径的方法java.nio.file.FileSystems 用于访问文件系统的类java.nio.file.FileSystem 代表了一种文件系统,例如Unix下的根目录为 / ,而Windows下则为C盘java.nio.file.FileStore 代表了真正的存储设备,提供了设备的详尽信息java.nio.file.attribute.FileStoreAttributeView 提供了访问文件的信息
一个路径可以确定一个文件或者文件夹的具体位置。Java7中用Path对象来实现对文件或者文件夹的操作。获得一个Path对象的方法有很多,主要有以下两种:
使用FileSystem对象的getPath方法使用Path对象的get方法
我们先来看看getPath方法,示例代码如下。
public static void main(String[] args) { Path path = FileSystems.getDefault().getPath("/Home/projects/node.txt"); System.out.println(); System.out.println("toString: " + path.toString()); System.out.printf("getFileName: %s\n", path.getFileName()); System.out.printf("getRoot: %s\n", path.getRoot()); System.out.printf("getNameCount: %d\n", path.getNameCount()); for (int index = 0; index < path.getNameCount(); index++) { System.out.printf("getName(%d): %s\n", index, path.getName(index)); } System.out.printf("subpath(0,2): %s\n", path.subpath(0, 2)); System.out.printf("getParent: %s\n", path.getParent()); System.out.println(path.isAbsolute()); try { path = Paths.get("Home", "projects", "users.txt"); System.out.printf("Absolute path: %s", path.toAbsolutePath()); } catch (InvalidPathException ex) { System.out.printf("Bad path: [%s] at position %s", ex.getInput(), ex.getIndex()); } }public static Path get(String first, String... more) { return FileSystems.getDefault().getPath(first, more);}public static void main(String[] args) { try { Path path = Paths.get(new URI("file:///C:/home/docs/users.txt")); File file = new File("C:\\home\\docs\\users.txt"); Path toPath = file.toPath(); System.out.println(toPath.equals(path)); } catch (URISyntaxException e) { System.out.println("Bad URI"); }} public static void main(String[] args) { String separator = FileSystems.getDefault().getSeparator(); System.out.println("The separator is " + separator); try { Path path = Paths.get(new URI("file:///D:/Home/projects/node.txt")); System.out.println("subpath: " + path.subpath(0, 3)); path = Paths.get("/home", "docs", "users.txt"); System.out.println("Absolute path: " + path.toAbsolutePath()); System.out.println("URI: " + path.toUri()); } catch (URISyntaxException ex) { System.out.println("Bad URI"); } catch (InvalidPathException ex) { System.out.println("Bad path: [" + ex.getInput() + "] at position " + ex.getIndex()); } }Path path = Paths.get(new URI("file:///C:/home/docs/bogusfile.txt"));System.out.println("File exists: " + Files.exists(path)); public static void main(String[] args) { Path path = Paths.get("D:/home/docs/../music/Space Machine A.mp3"); System.out.println("Absolute path: " + path.toAbsolutePath()); System.out.println("URI: " + path.toUri()); System.out.println("Normalized Path: " + path.normalize()); System.out.println("Normalized URI: " + path.normalize().toUri()); System.out.println(); path = Paths.get("D:/home/./music/ Robot Brain A.mp3"); System.out.println("Absolute path: " + path.toAbsolutePath()); System.out.println("URI: " + path.toUri()); System.out.println("Normalized Path: " + path.normalize()); System.out.println("Normalized URI: " + path.normalize().toUri()); } public static void main(String[] args) { Path path1 = null; Path path2 = null; path1 = Paths.get("/home/docs/users.txt"); path2 = Paths.get("/home/music/users.txt"); System.out.println(Files.isSymbolicLink(path1)); System.out.println(Files.isSymbolicLink(path2)); try { Path path = Paths.get("C:/home/./music/users.txt"); System.out.println("Normalized: " + path.normalize()); System.out.println("Absolute path: " + path.toAbsolutePath()); System.out.println("URI: " + path.toUri()); System.out.println("toRealPath (Do not follow links): " + path.toRealPath(LinkOption.NOFOLLOW_LINKS)); System.out.println("toRealPath: " + path.toRealPath()); Path firstPath = Paths.get("/home/music/users.txt"); Path secondPath = Paths.get("/docs/status.txt"); System.out.println("From firstPath to secondPath: " + firstPath.relativize(secondPath)); System.out.println("From secondPath to firstPath: " + secondPath.relativize(firstPath)); System.out.println("exists (Do not follow links): " + Files.exists(firstPath, LinkOption.NOFOLLOW_LINKS)); System.out.println("exists: " + Files.exists(firstPath)); System.out.println("notExists (Do not follow links): " + Files.notExists(firstPath, LinkOption.NOFOLLOW_LINKS)); System.out.println("notExists: " + Files.notExists(firstPath)); } catch (IOException ex) { Logger.getLogger(SymbolicLinkExample.class.getName()).log(Level.SEVERE, null, ex); } }