Java7中文件IO发生了很大的变化,专门引入了很多新的类:
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
……等等,来取代原来的基于java.io.File的文件IO操作方式.
Path
Path 用于来表示文件路径和文件。
construct method(构造方法)
- Paths.get(String first, String… more)
/* 源码
public static Path get(String first, String... more) {
return FileSystems.getDefault().getPath(first, more);
}
*/
Path path1 = Paths.get("C:\\Users\\Administrator\\Desktop\\test");
Path path2 = Paths.get("C:\\Users\\Administrator\\Desktop\\", "test.txt");
- Paths.get(URI uri)
/* 源码
public static Path get(URI uri) {
String scheme = uri.getScheme();
if (scheme == null)
throw new IllegalArgumentException("Missing scheme");
// check for default provider to avoid loading of installed providers
if (scheme.equalsIgnoreCase("file"))
return FileSystems.getDefault().provider().getPath(uri);
// try to find provider
for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
if (provider.getScheme().equalsIgnoreCase(scheme)) {
return provider.getPath(uri);
}
}
throw new FileSystemNotFoundException("Provider \"" + scheme + "\" not installed");
}
*/
URI uri = URI.create("file:///C:/Users/Administrator/Desktop/test")
- FileSystems.getDefault().getPath(String first, String… more)
从源码可以看出Path.get本质上是调用该方法的
Path path = FileSystems.getDefault().getPath("C:\\Users\\Administrator\\Desktop\\log.txt");
- file.toPath()
File file = new File("C:/Users/Administrator/Desktop/log.txt");
Path path = file.toPath();
File f = path.toFile();
URI uri = file.toURI();
path.isAbsolute() & path.toAbsolutePath()
An absolute path is complete in that it doesn’t need to be combined with other path information in order to locate a file.
一个绝对路径是完整的,因为它不需要与其他路径信息合并来定位一个文件。
也就是说,如果它是相对当前目录进行定位的,那就是相对路径,否则就是绝对路径。
Path path1 = Paths.get("C:\\Users\\Administrator\\Desktop");
Path path2 = Paths.get("C:\\Users\\..\\Windows");
Path path3 = Paths.get("..\\");
System.out.println(path1.toAbsolutePath() + " isAbsolute " + path1.isAbsolute());
System.out.println(path2.toAbsolutePath() + " isAbsolute " + path2.isAbsolute());
System.out.println(path3.toAbsolutePath() + " isAbsolute " + path3.isAbsolute());
输出
C:\Users\Administrator\Desktop isAbsolute true
C:\Users\..\Windows isAbsolute true
D:\workspace\Test\.. isAbsolute false
path.getRoot()
Returns the root component of this path as a Path object, or null if this path does not have a root component.
返回该路径的根组件作为一个路径对象,或者返回空,如果该路径没有一个根组件。
那么什么样的路径没有根组件呢?就是上面所说的相对路径。
Path path1 = Paths.get("C:\\Users\\Administrator\\Desktop");
Path path2 = Paths.get("C:\\Users\\..\\Windows");
Path path3 = Paths.get("..\\");
Path path4 = Paths.get("..\\workspace");
System.out.println(path1 + " getRoot=> " + path1.getRoot());
System.out.println(path2 + " getRoot=> " + path2.getRoot());
System.out.println(path3 + " getRoot=> " + path3.getRoot());
System.out.println(path4 + " getRoot=> " + path4.getRoot());
输出
C:\Users\Administrator\Desktop getRoot=> C:\
C:\Users\..\Windows getRoot=> C:\
.. getRoot=> null
..\workspace getRoot=> null
path.getFileName()
文件名一般是路径的最后一个名字组件。
注意以下两点:
- 如果该路径是绝对路径的根,那么它不存在文件名。
- 相对路径一定存在文件名,即使是“..”也可以当作文件名的。
Path path1 = Paths.get("C:\\Users\\Administrator\\Desktop");
Path path2 = Paths.get("C:\\Users\\..\\Windows");
Path path3 = Paths.get("C:\\");
Path path4 = Paths.get("C:");
Path path5 = Paths.get("..\\");
System.out.println(path1 + " getFileName=> " + path1.getFileName());
System.out.println(path2 + " getFileName=> " + path2.getFileName());
System.out.println(path3 + " getFileName=> " + path3.getFileName());
System.out.println(path4 + " getFileName=> " + path4.getFileName());
System.out.println(path5 + " getFileName=> " + path5.getFileName());
输出
C:\Users\Administrator\Desktop getFileName=> Desktop
C:\Users\..\Windows getFileName=> Windows
C:\ getFileName=> null
C: getFileName=> null
.. getFileName=> ..
path.getParent()
如果路径中只有一个名字组件,那么它没有父目录,否则有父目录。
要注意:如果路径的最后一个名字组件是“..”,那么getParent() 返回的结果与实际预期的会不一样。要将该路径转化为一个实际路径后(path.toRealPath(LinkOption… options))再使用该方法。
Path path1 = Paths.get("C:\\Users\\Administrator\\Desktop");
Path path2 = Paths.get("C:\\Users\\..\\Windows");
Path path3 = Paths.get("C:\\Users\\..");
Path path4 = Paths.get("C:\\");
Path path5 = Paths.get("..\\");
Path path6 = Paths.get("..\\workspace");
System.out.println(path1 + " getParent=> " + path1.getParent());
System.out.println(path2 + " getParent=> " + path2.getParent());
System.out.println(path3 + " getParent=> " + path3.getParent());
System.out.println(path4 + " getParent=> " + path4.getParent());
System.out.println(path5 + " getParent=> " + path5.getParent());
System.out.println(path6 + " getParent=> " + path6.getParent());
输出
C:\Users\Administrator\Desktop getParent=> C:\Users\Administrator
C:\Users\..\Windows getParent=> C:\Users\..
C:\Users\.. getParent=> C:\Users
C:\ getParent=> null
.. getParent=> null
..\workspace getParent=> ..
path.getNameCount() & path.getName(int) & path.subpath(int beginIndex, int endIndex)
Path path1 = Paths.get("C:\\Users\\Administrator\\Desktop");
Path path2 = Paths.get("C:\\Users\\..\\Windows");
Path path3 = Paths.get("..\\workspace");
System.out.println(path1 + " getNameCount=> " + path1.getNameCount());
System.out.println(path2 + " getNameCount=> " + path2.getNameCount());
System.out.println(path3 + " getNameCount=> " + path3.getNameCount());
System.out.println();
System.out.println(path1 + " getName(1)=> " + path1.getName(1));
System.out.println(path2 + " getName(1)=> " + path2.getName(1));
System.out.println(path3 + " getName(1)=> " + path3.getName(1));
System.out.println();
System.out.println(path1 + " subpath(1,2)=> " + path1.subpath(1,2));
System.out.println(path2 + " subpath(1,2)=> " + path2.subpath(1,2));
System.out.println(path3 + " subpath(1,2)=> " + path3.subpath(1,2));
输出
C:\Users\Administrator\Desktop getNameCount=> 3
C:\Users\..\Windows getNameCount=> 3
..\workspace getNameCount=> 2
C:\Users\Administrator\Desktop getName(1)=> Administrator
C:\Users\..\Windows getName(1)=> ..
..\workspace getName(1)=> workspace
C:\Users\Administrator\Desktop subpath(1,2)=> Administrator
C:\Users\..\Windows subpath(1,2)=> ..
..\workspace subpath(1,2)=> workspace
path.normalize()
Returns a path that is this path with redundant name elements eliminated.
返回一个消除了累赘的名字元素的路径。
该方法与path.toRealPath()的区别是:该方法不会解析以“..”打头的路径,即它利用的只是当前给定的路径信息。
Path path1 = Paths.get("C:\\Users\\..\\Windows");
Path path2 = Paths.get("C:\\Users\\..");
Path path3 = Paths.get("..\\");
Path path4 = Paths.get("..\\workspace");
System.out.println(path1 + " normalize=> " + path1.normalize());
System.out.println(path2 + " normalize=> " + path2.normalize());
System.out.println(path3 + " normalize=> " + path3.normalize());
System.out.println(path4 + " normalize=> " + path4.normalize());
输出
C:\Users\..\Windows normalize=> C:\Windows
C:\Users\.. normalize=> C:\
.. normalize=> ..
..\workspace normalize=> ..\workspace
path.toRealPath(LinkOption… options)
Returns the real path of an existing file.
将相对路径转化为其实际路径。
Path path1 = Paths.get("C:\\Users\\..\\Windows");
Path path2 = Paths.get("..\\");
try {
System.out.println(path1 + " toRealPath=> " + path1.toRealPath());
System.out.println(path2 + " toRealPath=> " + path2.toRealPath());
} catch (IOException e) {
e.printStackTrace();
}
输出
C:\Users\..\Windows toRealPath=> C:\Windows
.. toRealPath=> D:\workspace
path.resolve & path.resolveSibling
path.resolve(Path) 和 path.resolve(String) 在当前目录下拼接给定路径。
path.resolveSibling(Path) 和 path.resolveSibling(String) 在当前路径的父目录下拼接给定路径(即拼接在其兄弟节点上)
和 Node.js 上的 url 拼接一样用的都是 resolve 方法
Path path1 = Paths.get("C:\\Users");
Path path2 = Paths.get("Administrator\\Desktop");
Path path3 = path1.resolve(path2);
Path path4 = path1.resolve("Administrator\\Desktop");
Path path5 = path1.resolveSibling(path2);
Path path6 = path1.resolveSibling("Administrator\\Desktop");
System.out.println(path3);
System.out.println(path4);
System.out.println(path5);
System.out.println(path6);
输出
C:\Users.txt\Administrator\Desktop
C:\Users.txt\Administrator\Desktop
C:\Administrator\Desktop
C:\Administrator\Desktop
##path.toString()
输出Path指向的路径,分隔符用System.getProperty(“file.separator”)的值。
Path path1 = Paths.get("C:\\Users\\Administrator\\Desktop");
Path path2 = Paths.get("C:/Users/Administrator/Desktop");
System.out.println(path1.toString());
System.out.println(path2.toString());
System.out.println(System.getProperty("file.separator"));
输出
C:\Users\Administrator\Desktop
C:\Users\Administrator\Desktop
\