一.Paths
Path用来表示文件的路径
Paths是工具类,用来获取Path实例
.表示当前路径,..代表上一级路径
package com.guguo.test.pathandfiles;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Pathtest {
public static void main(String[] args) {
//已知在D盘根目录下放了个2.txt
Path path = Paths.get("1.txt");//相对路径
Path path1 = Paths.get("D:\\2.txt");//绝对路径1
Path path2 = Paths.get("D:/2.txt");//绝对路径2
Path path3 = Paths.get("D:","2.txt");//绝对路径3
//演示这个Paths支持.和..的功能
Path path4 = Paths.get("D:\\idea_saved\\Springboot_jicheng\\article.xml\\..\\1.txt");
System.out.println(path4.normalize());//D:\idea_saved\Springboot_jicheng\1.txt
}
}
二.Files
1.基本方法
package com.guguo.test.pathandfiles;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class FilesTest {
public static void main(String[] args) throws IOException {
//1.判断文件是否存在
Path path = Paths.get("1.txt");
System.out.println(Files.exists(path));
//2.创建单级目录。如果目录已经存在会抛出异常。也不能一次创建多级目录,不然会报NoSuchFileException
Path path2 = Paths.get("yahou/lalala");//此时会报错
Files.createDirectory(path2);
//3.创建多级目录
Path path3 = Paths.get("yahou/lalala");
Files.createDirectories(path3);
//4.复制文件,如果希望用source覆盖掉target,那么加StandardCopyOption.REPLACE_EXISTING
Path source = Paths.get("1.txt");
Path target = Paths.get("3.txt");
Files.copy(source,target);//这个方法如果target文件存在会报错
Files.copy(source,target, StandardCopyOption.REPLACE_EXISTING);//这个方法,文件存在就没事。
//5.移动文件
Files.move(source,target, StandardCopyOption.ATOMIC_MOVE);//这个参数是保证移动文件的原子性
//6.删除文件
Path source1 = Paths.get("4.txt");
Files.delete(source1);//如果文件不存在会报错NoSuchFileException
//7.删除目录
Path source2 = Paths.get("yahou/lalala");//如果目录下有内容会报错
Files.delete(source2);
}
}
2.遍历文件目录
package com.guguo.test.pathandfiles;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;
//遍历文件夹和目录下的文件
public class findFilesDemo {
public static void main(String[] args) throws IOException {
AtomicInteger dirNumber = new AtomicInteger();
AtomicInteger fileNumber = new AtomicInteger();
Files.walkFileTree(Paths.get("D:\\idea_saved\\Springboot_jicheng"),new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("进入了一个目录=>"+dir);
dirNumber.incrementAndGet();
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println(file);
fileNumber.incrementAndGet();
return super.visitFile(file, attrs);
}
});
System.out.println("文件夹数量是"+dirNumber);
System.out.println("文件数量是"+fileNumber);
}
}
3.删除多级目录
package com.guguo.test.pathandfiles;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
public class deleteFile {
public static void main(String[] args) throws IOException {
Files.walkFileTree(Paths.get("D:\\idea_saved\\mybatisX_demo - 副本"),new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return super.postVisitDirectory(dir, exc);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return super.visitFile(file, attrs);
}
});
}
}
4.复制多级目录
package com.guguo.test.pathandfiles;
import java.io.IOException;
import java.nio.file.*;
public class copyFiles {
public static void main(String[] args) throws IOException {
String source="D:\\idea_saved\\copy_used";
String target="D:\\idea_saved\\copy_used - 副本";
Files.walk(Paths.get(source)).forEach(path -> {
try {
String targetName=path.toString().replace(source,target);
if(Files.isDirectory(path)){//如果是目录那么创建目录
Files.createDirectory(Paths.get(targetName));
}else if(Files.isRegularFile(path)){//如果是文件,打印文件
Files.copy(path,Paths.get(targetName));
}
}catch (IOException e){
e.printStackTrace();
}
});
}
}