当前位置: 首页 > article >正文

NIO笔记03-文件编程

文章目录

  • 1 FileChannel
    • 获取
    • 读取
    • 写入
    • 关闭
    • 位置
    • 大小
    • 强制写入
  • 2 两个 Channel 传输数据
  • 3 Path
  • 4 Files
    • 检查文件是否存在
    • 创建一级目录
    • 创建多级目录用
    • 拷贝文件(和transferTo效率相仿)
    • 移动文件
    • 删除文件
    • 删除目录
    • 遍历目录文件
    • 拷贝多级目录


1 FileChannel

⚠️ FileChannel 工作模式:
FileChannel 只能工作在阻塞模式下

获取

不能直接打开 FileChannel,必须通过 FileInputStream、FileOutputStream 或者 RandomAccessFile 来获取 FileChannel,它们都有 getChannel 方法。

  • 通过 FileInputStream 获取的 channel 只能读
  • 通过 FileOutputStream 获取的 channel 只能写
  • 通过 RandomAccessFile 是否能读写根据构造 RandomAccessFile 时的读写模式决定

读取

会从 channel 读取数据填充 ByteBuffer,返回值表示读到了多少字节,-1 表示到达了文件的末尾

int readBytes = channel.read(buffer);

写入

写入的正确姿势如下, SocketChannel

ByteBuffer buffer = ...;
buffer.put(...); // 存入数据
buffer.flip();   // 切换读模式

while(buffer.hasRemaining()) {
    channel.write(buffer);
}

在 while 中调用 channel.write 是因为 write 方法并不能保证一次将 buffer 中的内容全部写入 channel

关闭

channel 必须关闭,不过调用了 FileInputStream、FileOutputStream 或者 RandomAccessFile 的 close 方法会间接地调用 channel 的 close 方法

位置

获取当前位置

long pos = channel.position();

设置当前位置

long newPos = ...;
channel.position(newPos);

设置当前位置时,如果设置为文件的末尾

  • 这时读取会返回 -1
  • 这时写入,会追加内容,但要注意如果 position 超过了文件末尾,再写入时在新内容和原末尾之间会有空洞(00)

大小

使用 size 方法获取文件的大小

强制写入

操作系统出于性能的考虑,会将数据缓存,不是立刻写入磁盘。可以调用 force(true) 方法将文件内容和元数据(文件的权限等信息)立刻写入磁盘

2 两个 Channel 传输数据

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class TestFileChannelTransferTo {
    public static void main(String[] args) {
        try (
                FileChannel from = new FileInputStream("data.txt").getChannel();
                FileChannel to = new FileOutputStream("to.txt").getChannel();
        ) {
            // 效率高,底层会利用操作系统的零拷贝进行优化, 但2G以上数据读取到2G数据就会停止
            //data.txt向to.txt传输数据
//            from.transferTo(0,from.size(),to);

            //2G以上数据
            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);//transferTo返回传递的实际传输的字节数
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

实际传输一个超大文件输出:

position:0 left:7769948160
position:2147483647 left:5622464513
position:4294967294 left:3474980866
position:6442450941 left:1327497219

3 Path

jdk7 引入了 Path 和 Paths 类

  • Path 用来表示文件路径
  • Paths 是工具类,用来获取 Path 实例
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
  • . 代表了当前路径
  • .. 代表了上一级路径

例如目录结构如下

d:
	|- data
		|- projects
			|- a
			|- b

代码

Path path = Paths.get("d:\\data\\projects\\a\\..\\b");
System.out.println(path);
System.out.println(path.normalize()); // 正常化路径

会输出

d:\data\projects\a\..\b
d:\data\projects\b

4 Files

检查文件是否存在

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);

拷贝文件(和transferTo效率相仿)

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

遍历目录文件

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

/**
 * 遍历文件夹中的文件和删除文件
 */
public class Test02FilesWalkFileTree {
    public static void main(String[] args) throws IOException {
//        m1();
        m2();

        //当文件夹不为空时,Files.delete是无法直接删除的
//        Files.delete(Paths.get("D:\\xxx"));
        //m3();

    }

    /**
     * 删除多级目录
     * 递归删除文件夹中所有的文件
     * @throws IOException
     */
    private static void m3() throws IOException {
        Files.walkFileTree(Paths.get("D:\\Snipaste-1.16.2-x64 - 副本"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return super.visitFile(file, attrs);
            }
            //从文件夹出来之后访问该方法
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
    }
拷贝多级目录
    /**
     * 找到文件夹中.jar后缀的文件
     * @throws IOException
     */
    private static void m2() throws IOException {
        AtomicInteger jarCount = new AtomicInteger();
        Files.walkFileTree(Paths.get("E:\\tool\\JDK11"), new SimpleFileVisitor<Path>(){
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.toString().endsWith(".jar")) {
                    System.out.println(file);
                    jarCount.incrementAndGet();
                }
                return super.visitFile(file, attrs);
            }
        });
        System.out.println("jar count:" +jarCount);//jar count:1
    }

    /**
     * 打印文件夹和文件
     * @throws IOException
     */
    private static void m1() throws IOException {
        AtomicInteger dirCount = new AtomicInteger();
        AtomicInteger fileCount = new AtomicInteger();
        //这里使用了访问者模式
        // Files.walkFileTree进行遍历,遍历到需要的参数使用SimpleFileVisitor进行操作
        Files.walkFileTree(Paths.get("E:\\tool\\JDK11"), 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("dir count:" +dirCount);//dir count:89
        System.out.println("file count:" +fileCount);//file count:421
    }
}

拷贝多级目录

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 拷贝文件夹
 */
public class Test03FilesCopy {

    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        String source = "E:\\tool\\JDK11";
        String target = "E:\\tool\\JDK11test";

        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();
            }
        });
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}


http://www.kler.cn/news/289185.html

相关文章:

  • JS实现高度不等的列表虚拟滚动加载
  • mysql迁移到达梦数据库报错:列[xx]长度超出定义
  • subclass-balancing的related work+conclusion
  • 智能合约漏洞(五)
  • AI大模型编写多线程并发框架(六十五):发布和应用
  • vue 动态替换父组件
  • salesforce flow 更新记录,某一个更新失败会导致所有失败吗
  • SpringMVC基于注解的使用
  • 系统编程-数据库
  • [开源]低代码表单FormCreate的control表单联动功能的详解
  • vue3中使用supermap icilent3d for cesium
  • 【深度学习 CV方向】图像算法工程师 职业发展路线,以及学习路线
  • SSMA for MySQL 将MySQL数据导入SqlServer
  • Xilinx FFT IP使用
  • 【流式输出】LangChain流式输出的概念
  • Python教程:使用 Python 和 PyHive 连接 Hive 数据库
  • windows核心编程,纤程使用---->UNIX服务器应用程序移植到Windows中
  • 在更新python虚拟环境pip版本时,由于更新失败导致pip丢失的修复
  • GD - EmbeddedBuilder - 给已有工程换MCU
  • 【IPV6从入门到起飞】2-1 获取你的IPV6(手机、CPE等)
  • LVS Keepalived nginx haproxy 区别
  • 设计模式-原型适配器桥接外观
  • 基于大数据分析景区消费行为影响因素研究【消费等级预测、携程,去哪网数据抓取】
  • 亿图图示下载安装教程EdrawMax Pro 13版超详细图文教程
  • 前端面试:webSocket如何兼容低浏览器?
  • 安防监控视频平台LntonAIServer视频智能分析平台新增视频质量诊断功能
  • 机器学习数学公式推导之降维
  • Python加载 TorchScript 格式的 ResNet18 模型分类该模型进行预测并输出预测的类别和置信度
  • 【运维监控】prometheus+node exporter+grafana 监控linux机器运行情况(2)
  • 【wsl2】从C盘迁移到G盘