1、Path
jdk7 引入了 Path 和 Paths 类
- Path 用来表示文件路径
- Paths 是工具类,用来获取 Path 实例
```java Path source = Paths.get(“1.txt”); // 相对路径 使用 user.dir 环境变量来定位 1.txt
Path source = Paths.get(“d:\1.txt”); // 绝对路径 代表了 d:\1.txt
Path source = Paths.get(“d:/1.txt”); // 绝对路径 同样代表了 d:\1.txt
Path projects = Paths.get(“d:\data”, “projects”); // 代表了 d:\data\projects
<a name="c91V3"></a>
# 2、Files
**检查文件是否存在**
```java
Path path = Paths.get("helloword/data.txt");
System.out.println(Files.exists(path));
创建一级目录
Path path = Paths.get("helloword/d1");
Files.createDirectory(path);
- 如果目录已存在,会抛异常 FileAlreadyExistsException
- 不能一次创建多级目录,否则会抛异常 NoSuchFileException
创建多级目录
Path path = Paths.get("helloword/d1/d2");
Files.createDirectories(path);
拷贝文件
Path source = Paths.get("helloword/data.txt");
Path target = Paths.get("helloword/target.txt");
Files.copy(source, target);
- 如果文件已存在,会抛异常 FileAlreadyExistsException
如果希望用 source 覆盖掉 target,需要用 StandardCopyOption 来控制
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
移动文件
Path source = Paths.get("helloword/data.txt");
Path target = Paths.get("helloword/data.txt");
Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
- StandardCopyOption.ATOMIC_MOVE 保证文件移动的原子性
删除文件
Path target = Paths.get("helloword/target.txt");
Files.delete(target);
- 如果文件不存在,会抛异常 NoSuchFileException
删除目录**
Path target = Paths.get("helloword/d1");
Files.delete(target);
- 如果目录还有内容,会抛异常 DirectoryNotEmptyException
遍历目录
public static void visitDir(String targetDir) throws IOException {
AtomicInteger dirCount = new AtomicInteger(0);
AtomicInteger fileCount = new AtomicInteger(0);
Files.walkFileTree(Paths.get(targetDir),new SimpleFileVisitor<>(){
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("======>" + dir);
dirCount.addAndGet(1);
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println(file);
fileCount.addAndGet(1);
return super.visitFile(file, attrs);
}
});
System.out.println("目录数量:" + dirCount);
System.out.println("文件数量:" + fileCount);
}
删除多级目录
public static void delDir(String targetDir) throws IOException {
Files.walkFileTree(Paths.get(targetDir),new SimpleFileVisitor<>(){
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("======>" + dir);
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println(file);
Files.delete(file);
return super.visitFile(file, attrs);
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
System.out.println("<======" + dir);
Files.delete(dir);
return super.postVisitDirectory(dir, exc);
}
});
}
拷贝目录
public static void copyDir(String sourceDir,String targetDir) throws IOException {
Files.walk(Paths.get(sourceDir)).forEach(path -> {
try {
String newPath = path.toString().replace(sourceDir, targetDir);
if(Files.isDirectory(path)){
Files.createDirectory(Paths.get(newPath));
}else if(Files.isRegularFile(path)){
Files.copy(path,Paths.get(newPath));
}
} catch (IOException e) {
e.printStackTrace();
}
});
}