解决io流读写文件覆盖问题

本文介绍了如何使用Java的BufferedInputStream和BufferedOutputStream进行文件的读写操作,通过缓冲区提升性能。示例代码展示了如何创建缓冲流并进行读写,强调了在追加模式下写入的设置。
package IO;

import java.io.*;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description: (用一句话描述该文件做什么)
 * @author: cccwn
 * @From: https://blog.csdn.net/cccwn?type=blog
 * @date
 */
public class BufferedInputStreamDome {
    //
    public static void main(String[] args) throws IOException {
        //字节缓冲流自带8kb的缓冲区所以性能比较io流要快
        //写数据
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myStream\\2323.txt"));
        bos.write("abc\r\n".getBytes());
        bos.write("qwe\r\n".getBytes());
        bos.write("bnm\r\n".getBytes());
        bos.close();
        //读数据
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("myStream\\2323.txt"));
        byte[] bytes = new byte[1024];
        int by;
        while ((by = bis.read(bytes)) != -1){//读取1k字节
            //System.out.println(by);读取字节
            //转化成字符串
            //String(byte[] bytes, int offset, int length, String charsetName)
            //构造一个新的 String通过使用指定的字符集解码指定的字节子阵列。
            String s = new String(bytes, 0, by,"utf-8");
            System.out.println(s);
        }bis.close();


    }
}

读写问题只要在true就可解决

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myStream\\2323.txt",true));
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值