jdk7 引入了 Path 和 Paths 类,Path 用来表示文件路径,Paths 是工具类,用来获取 Path 实例
1、相对路径定位 test.txt
Path source = Paths.get("test.txt");
2、绝对路径定位 d:\test.txt
Path source = Paths.get("d:\\test.txt");
3、绝对路径定位d:\data\test
Path path = Paths.get("d:\\data", "test");
1、检查文件是否存在
Path path = Paths.get("src/main/resource/nio/test2.txt");
boolean exists = Files.exists(path);
2、创建目录
Path path = Paths.get("src/main/resource/nio/test");
Files.createDirectory(path);
在src/main/resource/nio目录下创建新目录test
注意:
(1)如果目录已存在,会抛异常 FileAlreadyExistsException
(2)不能一次创建多级目录,否则会抛异常 NoSuchFileException
3、创建多级目录用
Path path = Paths.get("src/main/resource/nio/first/second");
Files.createDirectory(path);
4、复制文件
Path fromPath = Paths.get("src/main/resource/nio/test2.txt");
Path toPath = Paths.get("src/main/resource/nio/to.txt");
Files.copy(fromPath,toPath, StandardCopyOption.REPLACE_EXISTING);
如果希望用 source 覆盖掉 target,需要用 StandardCopyOption 来控制
5、移动文件
Path fromPath = Paths.get("src/main/resource/nio/test2.txt");
Path toPath = Paths.get("src/main/resource/nio/to2.txt");
Files.move(fromPath,toPath,StandardCopyOption.ATOMIC_MOVE);
StandardCopyOption.ATOMIC_MOVE 保证文件移动的原子性
6、删除文件
Path path = Paths.get("src/main/resource/nio/to.txt");
Files.delete(path);
如果文件不存在,会抛异常 NoSuchFileException
7、删除目录
Path path = Paths.get("src/main/resource/nio/test");
Files.delete(path);
8、遍历目录
Path path = Paths.get("src/main/resource/nio");
Files.walkFileTree(path,new SimpleFileVisitor<Path>(){
//访问文件前处理
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("before dir="+dir);
return super.preVisitDirectory(dir, attrs);
}
//访问文件处理
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println(file);
return super.visitFile(file, attrs);
}
//访问文件后处理
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
System.out.println("after dir="+dir);
return super.postVisitDirectory(dir, exc);
}
//访问文件失败处理
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
System.out.println("exception file="+file);
return super.visitFileFailed(file, exc);
}
});
before dir=src\main\resource\nio
src\main\resource\nio\test.txt
src\main\resource\nio\test2.txt
after dir=src\main\resource\nio
String FROM = "helloword/data.txt";
String TO = "helloword/to.txt";
long start = System.nanoTime();
try (
FileChannel from = new FileInputStream(FROM).getChannel();
FileChannel to = new FileOutputStream(TO).getChannel();
) {
from.transferTo(0, from.size(), to);
} catch (IOException e) {
e.printStackTrace();
}
long end = System.nanoTime();
System.out.println("transferTo 用时:" + (end - start) / 1000_000.0);
超过 2g 大小的文件传输
public class TestFileChannelTransferTo {
public static void main(String[] args) {
try (
FileChannel from = new FileInputStream("data.txt").getChannel();
FileChannel to = new FileOutputStream("to.txt").getChannel();
) {
// 效率高,底层会利用操作系统的零拷贝进行优化
long size = from.size();
// left 变量代表还剩余多少字节
for (long left = size; left > 0; ) {
System.out.println("position:" + (size - left) + " left:" + left);
left -= from.transferTo((size - left), left, to);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}