Path
path接口是在java7加入到NIO的,位于java.nio.file包,它的实例代表了文件系统里的一个路径
初始化
Path path = Paths.get("D:\\svn-config.properties");
Path.normalize()
normalize()方法可以标准化一个路径,就是移除掉所有在路径字符中的的 .
和 ..
,同时决定路径字符串指向哪条路径.
Path path = Paths.get("D:\\Users\\xxx\\Desktop\\..\\svn-config.properties");
Path path1 = path.normalize();
System.out.println(path);
System.out.println(path1);
结果:
D:\Users\xxx\Desktop\..\svn-config.properties
D:\Users\xxx\svn-config.properties
Files
Java NIO File 类提供了几个在文件系统中熟练操作文件的方法
判断文件存在
LinkOption.NOFOLLOW_LINKS
表明判断文件存在的方法不应该根据文件系统中的符号连接来判断路径是否存在
Path path = Paths.get("D:\\Users\\xxx\\Desktop\\svn-config.properties");
boolean isExist = Files.exists(path, LinkOption.NOFOLLOW_LINKS);
创建文件夹
Path path = Paths.get("D:\dir");
try
{
Path newDir = Files.createDirectory(path);
} catch(FileAlreadyExistsException e){
// the directory already exists.
} catch (IOException e) {
//something else went wrong
e.printStackTrace();
}
拷贝文件
Path sourcePath = Paths.get("data/logging.properties");
Path destinationPath = Paths.get("data/logging-copy.properties");
try {
Files.copy(sourcePath, destinationPath);
} catch(FileAlreadyExistsException e) {
//destination file already exists
} catch (IOException e) {
//something else went wrong
e.printStackTrace();
}
覆盖文件
Path sourcePath = Paths.get("data/logging.properties");
Path destinationPath = Paths.get("data/logging-copy.properties");
try {
Files.copy(sourcePath, destinationPath,
StandardCopyOption.REPLACE_EXISTING);
} catch(FileAlreadyExistsException e) {
//destination file already exists
} catch (IOException e) {
//something else went wrong
e.printStackTrace();
}
移动文件
Path sourcePath = Paths.get("data/logging-copy.properties");
Path destinationPath = Paths.get("data/subdir/logging-moved.properties");
try {
Files.move(sourcePath, destinationPath,StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
//moving file failed.
e.printStackTrace();
}
删除文件
Path path = Paths.get("data/subdir/logging-moved.properties");
try {
Files.delete(path);
} catch (IOException e) {
//deleting file failed
e.printStackTrace();
}
遍历文件夹
Path path = Paths.get("D:\\mnt");
Files.walkFileTree(path, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("pre visit dir:" + dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("visit file: " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
System.out.println("visit file failed: " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
System.out.println("post visit directory: " + dir);
return FileVisitResult.CONTINUE;
}
});
查找文件
Path root = Paths.get("D:\\mnt");
final String fileToFind = File.separator+"path.txt";
try {
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String fileString = file.toAbsolutePath().toString();
//System.out.println("pathString = " + fileString);
if(fileString.endsWith(fileToFind)){
System.out.println("file found at path: " + file.toAbsolutePath());
return FileVisitResult.TERMINATE;
}
return FileVisitResult.CONTINUE;
}
});
} catch(IOException e){
e.printStackTrace();
}
循环删除文件
Path rootPath = Paths.get("data/to-delete");
try {
Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("delete file: " + file.toString());
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
System.out.println("delete dir: " + dir.toString());
return FileVisitResult.CONTINUE;
}
});
} catch(IOException e){
e.printStackTrace();
}
生成超链接
//连接路径
Path p = Paths.get("D:\\mnt\\1.txt");
//目标路径
Path target = Paths.get("D:\\Users\\weizhiming\\Desktop");
//生成指向目标路径的超链接,返回连接路径p
Path p3 = Files.createSymbolicLink(p,target);