Java7中對文件管理提供了大量的新API,這些新的接口可使咱們操縱文件以及文件夾更加方便。它們大多位於java.nio.file包下。
java
一個路徑能夠肯定一個文件或者文件夾的具體位置。Java7中用Path對象來實現對文件或者文件夾的操做。得到一個Path對象的方法有不少,主要有如下兩種:
node
咱們先來看看getPath方法,示例代碼以下。
code
Java代碼 orm
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()); } }
示例中FileSystems的getDefault方法,會由JVM返回一個表明了當前文件系統的FileSystem對象,咱們能夠經過FileSystem來得到Path對象。一個Path能夠由多個子Path組成,子Path能夠可通用過subpath方法來得到。
使用Paths類的get方法一樣能夠得到一個Path對象,若是你查看JDK的源碼你會發現,它的實現方式和用FileSystem是同樣的。
對象
Java代碼 接口
public static Path get(String first, String... more) { return FileSystems.getDefault().getPath(first, more); }
若是咱們使用的系統中有大量Java7以前對文件操做的代碼,例如使用了不少File類的話,咱們能夠經過Java7中對File類新增的toPath方法來將一個File轉換成Path。
get
Java代碼 源碼
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"); } }
將一個相對路徑轉換成絕對路徑,只須要調用Path類的toAbsolutePath方法。此外若是想要得到系統的路徑分隔符,FileSystem也提供了getSeparator方法。
it
Java代碼 io
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()); } }
判斷一個文件是否真實存在,在Files類中提供了exists方法,它接收一個Path對象做爲參數。
Java代碼
Path path = Paths.get(new URI("file:///C:/home/docs/bogusfile.txt")); System.out.println("File exists: " + Files.exists(path));
當路徑描述符中有"."或者".."字符時,咱們能夠經過調用Path的normalize方法將這些描述符轉換成真正的路徑。運行下面的代碼,你會發現"."都被刪除掉了,由於它表明當前路徑。".."會用上一層路徑代替。
Java代碼
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()); }
Path的toRealPath會返回一個真實表明一個文件的路徑,若是所指定的文件不存在,NoSuchFileException異常會被拋出。toRealPath能夠傳入一個LinkOption.NOFOLLOW_LINKS參數來將結果以連接的路徑顯示。Files判斷文件是否存在的方法也能夠加入此參數。下面的例子中music文件夾下的users是docs文件下users文件的連接。
Java代碼
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); } }
Java7中新引用的這些類的確大大簡化了咱們進行文件操做,使得操做文件再也不那麼繁瑣。