java中一些文件的相关操作

一、复制文件

使用FileChannel的 transferTo方法,可以复制文件,效率比较高,但是最大复制2G的文件,所以使用的时候,尽量使用循环的方式。

package com.test.c2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

public class FileTransferTo {

    public static void main(String[] args) {
        try (
                FileInputStream inputStream = new FileInputStream("data.txt");
                FileOutputStream outputStream = new FileOutputStream("data1.txt");
                FileChannel inputStreamChannel = inputStream.getChannel();
                FileChannel outputStreamChannel = outputStream.getChannel();
                ){
            long size = inputStreamChannel.size();
            long capacity = inputStreamChannel.size();
            // 分多次传输
            while (capacity > 0) {
                // transferTo返回值为传输了的字节数
                capacity -= inputStreamChannel.transferTo(size-capacity, capacity, outputStreamChannel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

二、Files工具类

Path和Paths

根据java的命名规则,可以知道Path就是代表一个路径,Paths就是创建Path的工具类。
 

package com.test.c2;

import java.nio.file.Path;
import java.nio.file.Paths;

public class PathTest {

    public static void main(String[] args) {
        // 相对路径 不带盘符 使用 user.dir 环境变量来定位 1.txt
        Path source1 = Paths.get("1.txt");
        // 绝对路径 代表了  d:\1.txt 反斜杠需要转义
        Path source2 = Paths.get("d:\\1.txt");
        // 绝对路径 同样代表了  d:\1.txt
        Path source3 = Paths.get("d:/1.txt");
        // 代表了  d:\data\projects
        Path projects4 = Paths.get("d:\\data", "projects");
    }

}

Files

主要API:

  • Files.exists(path):文件是否存在
  • Files.createDirectory(path):创建一级目录
  • Files.createDirectories(path):创建多级目录
  • Files.copy(source, target):拷贝文件
  • Files.move(source, target, StandardCopyOption.ATOMIC_MOVE):移动(第三个参数保证原子性)
  • Files.delete(target):删除,但是无法删除存在内容的文件夹
  • Files.walkFileTree:遍历访问文件夹,不用自己递归手动写了
package com.test.c2;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

public class FileWalkFileTree {

    public static void main(String[] args) throws Exception{
        Path path = Paths.get("E:\\AnkiPC-DiegoDad");
        // 文件目录数目
        AtomicInteger dirCount = new AtomicInteger();
        // 文件数目
        AtomicInteger fileCount = new AtomicInteger();
        Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("===>"+dir);
                // 增加文件目录数
                dirCount.incrementAndGet();
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println(file);
                // 增加文件数
                fileCount.incrementAndGet();
                return super.visitFile(file, attrs);
            }
        });

        System.out.println("文件数:" + fileCount);
        System.out.println("文件夹书:" + dirCount);
    }
}
使用了访问者模式,使用者可以在访问文件夹前、访问文件、访问文件失败、访问文件夹之后进行相关操作。

猜你喜欢

转载自blog.csdn.net/liming0025/article/details/119810194