字符流的读写操作

1.字符流是什么
字符流是可以直接读写字符的IO流
字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写出.
2.字符流读取写入文件都有这两种
单独用FileWriter写入文件会每次写入数据,磁盘都有一次写入导致效率低,使用BufferReader搭配使用会把缓冲区装满再进行写入,提高了写入的效率。
 

try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("aa1.txt", true));
            for (int i = 0; i < 10; i++) {
                bw.append("holle world!中文效果\n");
            }
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("字符流:");
        var m1 = System.currentTimeMillis();
        try {
            BufferedReader br = new BufferedReader(new FileReader("aa1.txt"));
            while (br.ready()) {
                System.out.println(br.readLine());
            }
            System.out.printf("字符流时间:%d毫秒%n", System.currentTimeMillis() - m1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

java.io.InputStream 输入流,主要是用来读取文件内容的。
 

java.io.OutputStream 输出流,主要是用来将内容字节写入文件的
 

System.out.println("字节流:");
        try {
            var m2 = System.currentTimeMillis();
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("aa1.txt"));
            byte[] s = new byte[1024];
            int aa = 0;
            while ((aa = bis.read(s)) > 0) {
                System.out.println(new String(s, 0, aa));
            }
            bis.close();
 
            System.out.printf("字节流时间:%d毫秒%n", System.currentTimeMillis() - m2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

byte[] s就是建立缓冲区读取字节
 

3.综合案例:

package xxx.yyy.zzz;

import java.io.*;

/**
 * 以字符流读取文件和写入文件,大部分针对文本文件操作,mp4视频等文件是字节,通过字节流方式操作
 */
public class IsrAndOswDemo {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("D:\\java\\java_io_file\\demo\\test.txt");
        InputStreamReader isr = new InputStreamReader((in));//默认项目的编码

        FileOutputStream out = new FileOutputStream("D:\\java\\java_io_file\\demo\\desFile.txt");
        OutputStreamWriter osw = new OutputStreamWriter(out);


        /*
        int c;
        while ((c = isr.read())!=-1){
            System.out.println((char)c);
        }
        
         */

        char[] buffer = new char[8*1024];
        int c1;
        //批量读取,放入buffer这个字符数组,从第0个位置开始防止,最多放buffer.legnth,返回的是读到的字符的个数
        while ((c1=isr.read(buffer, 0, buffer.length))!= -1){
            String s = new String(buffer, 0, c1);
            System.out.println(s);
            osw.write(s);
            //osw.write(buffer, 0, c1);
            osw.flush();
        }
        isr.close();
        osw.close();

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值