文件操作—文件内容交换及追加

本文介绍了如何使用Java的节点流(FileInputStream和FileOutputStream)以及缓冲流(BufferedInputStream和BufferedOutputStream)进行文件内容的复制和追加操作。通过实例展示了如何高效地交换文件内容,并演示了如何在追加模式下避免覆盖原始内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件操作—文件内容交换及追加

- 文章开始前首先让我们先整理一下一些流的分类和特点

节点流和处理流

一、节点流类型

这里我们只讨论我们现在所面对的文件IO流
在这里插入图片描述
(图片转载自网络)
二、节点流执行的图示
在这里插入图片描述
(图片转载自网络)
File 文件流:对文件进行读、写操作 :FileReader、FileWriter、FileInputStream、FileOutputStream。

三、处理流类型
在这里插入图片描述
四、处理流执行的图示
在这里插入图片描述
(图片转载自网络)
Buffering缓冲流:在读入或写出时,对数据进行缓存,以减少I/O的次数:BufferedReader与BufferedWriter、BufferedInputStream与BufferedOutputStream

FileInputstream 读取数据效率一般不是特别高;是一个一个字节读取
BufferedInputStream读取数据高效;一个数组一个数组的读取

文件内容交换

在这里插入图片描述

“复制”操作代码实现

 BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        bis = new BufferedInputStream(new FileInputStream(file1));
        bos = new BufferedOutputStream(new FileOutputStream(file2));
        int length = 0;
        byte[] buf = new byte[1024];
        while ((length = bis.read(buf)) != -1) {
            bos.write(buf, 0, length);
        }

核心代码:

    byte[] buf = new byte[1024];
    while ((length = bis.read(buf)) != -1) {
        bos.write(buf, 0, length);
    }     
    将文件内容储存为字节型数组,进行输入和输出写入另一个文件中

完整代码

public class SwapFile {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入你要交换内容的两个文件的路径:");
        String file1 = input.next();
        String file2 = input.next();
        String tempFile = "temp.txt";
        File temp = new File(tempFile);
        swapFile(file1, tempFile);
        swapFile(file2, file1);
        swapFile(tempFile, file2);
        System.out.println("-----已完成交换-----");
            if(temp.exists())
            temp.delete();
    }

    public static void swapFile(String file1,String file2) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try{
            bis = new BufferedInputStream(new FileInputStream(file1));
            bos = new BufferedOutputStream(new FileOutputStream(file2));
            int length = 0;
            byte[] buf = new byte[1024];
            while ((length = bis.read(buf)) != -1) {
                bos.write(buf, 0, length);
            }
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try {
                if(bos != null)
                bos.close();
                if(bis != null)
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

文件内容追加

public class AddFile {
    public static void main(String[] args) throws IOException {
        Scanner input =new Scanner(System.in);
        System.out.println("请输入你要追加内容的两个文件(前者加入后者)的路径:");
        String file1 = input.next();
        String file2 = input.next();
        addFile(file1,file2);
        System.out.println("-----已完成追加-----");
    }
    public static void addFile(String file1,String file2) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try{
        bis = new BufferedInputStream(new FileInputStream(file1));
        bos = new BufferedOutputStream(new FileOutputStream(file2,true));
        int length = 0;
        byte[] buf = new byte[1024];
        while ((length = bis.read(buf)) != -1) {
            bos.write(buf, 0, length);
        }
        }catch(Exception e){
		e.printStackTrace;
		}
        }finally{
        bos.close();
        bis.close();
        }
    }
}

与上述文件内容交换十分类似并且要更加简单

bis = new BufferedInputStream(new FileInputStream(file1));
bos = new BufferedOutputStream(new FileOutputStream(file2,true));

file1的内容写入file2文件时设定appendtrue(默认为false)即可实现添加内容时不覆盖原有内容,而是在末尾补充内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

之墨_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值