io字节流和字符流的部分实践

参考:字节流与字符流
Java 常用IO流操作详解
先上代码:(如有错误请指正)
主要是看不同的io写入 控制台和文本输出的不同

读取的文件内容,摘选百度到的新闻
在这里插入图片描述

import java.io.*;

public class IoTest {

    public static void main(String[] args) {
        IoTest iot = new IoTest();
        System.out.println("Method1:");
        iot.iotestMethod1();
        System.out.println("Method2:");
        iot.iotestMethod2();
        System.out.println("Method3:");
        iot.iotestMethod3();
        System.out.println("Method4:");
        iot.iotestMethod4();
    }

    public void iotestMethod1() {
        OutputStream os = null;
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            StringBuilder sb = new StringBuilder();
            FileOutputStream fos = new FileOutputStream("C:\\Users\\86135\\Desktop\\method1.txt");

            is = new FileInputStream("C:\\Users\\86135\\Desktop\\123.txt");
            isr = new InputStreamReader(is, "UTF-8");

            byte by[] = new byte[1024];
            int num = 0;
            while ((num = is.read(by)) != -1) {
                sb.append(new String(by, 0, num));
                fos.write(by, 0, num);
            }
            System.out.println(sb.toString());
        }catch (Exception e){

        }
    }
    public void iotestMethod2() {
        OutputStream os = null;
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;

        try {
            StringBuilder sb = new StringBuilder();
            is = new FileInputStream("C:\\Users\\86135\\Desktop\\123.txt");
            FileOutputStream fos = new FileOutputStream("C:\\Users\\86135\\Desktop\\method2.txt");
            isr = new InputStreamReader(is,"UTF-8");
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            char by2[] = new char[1024];
            int num2 = 0;
            while ((num2 = isr.read(by2))!=-1){
                sb.append(new String(by2,0,num2));
                osw.write(new String(by2,0,num2));
            }
            osw.close();
            System.out.println(sb);
        }catch (Exception e){

        }
    }
    public void iotestMethod3() {
        OutputStream os = null;
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;

        try {
            byte by3[] = new byte[1024];
            int num3 = 0;
            StringBuilder sb3 = new StringBuilder();
            is = new FileInputStream("C:\\Users\\86135\\Desktop\\123.txt");
            FileOutputStream fos = new FileOutputStream("C:\\Users\\86135\\Desktop\\method3.txt");

            BufferedInputStream bis = new BufferedInputStream(is);
            while ((num3 = bis.read(by3))!=-1){
                sb3.append(new String(by3,0,num3));
                fos.write(by3,0,num3);
            }
            System.out.println(sb3);

        }catch (Exception e){

        }
    }

    public void iotestMethod4() {
        OutputStream os = null;
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;

        try {
            StringBuilder sb4 = new StringBuilder();

            is = new FileInputStream("C:\\Users\\86135\\Desktop\\123.txt");
            FileOutputStream fos = new FileOutputStream("C:\\Users\\86135\\Desktop\\method4.txt");
            OutputStreamWriter osw = new OutputStreamWriter(fos);

            BufferedWriter bw = new BufferedWriter(osw);

            isr = new InputStreamReader(is,"UTF-8");
            br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                sb4.append(line);
                osw.write(line);
            }
            osw.close();
            String xml = sb4.toString();
            System.out.println(xml);
        }catch (Exception e){
        }
    }
}

结果展示

在这里插入图片描述
在这里插入图片描述
控制台的输出 结果比较
method1和method3 有中文乱码(字节数组的大小一次把一个完整的汉字字节没有读完)

//一次读取完,就不会出现乱码的问题
 File file = new File("C:\\Users\\86135\\Desktop\\123.txt");
 byte by[] = new byte[(int)file.length()];

method2 结果优秀
method4结构破坏

写入到文本的输出 结果比较

method1、method2 和method3 无差别 文档大小均为 1.48kb
method4结构破坏

在这里插入图片描述
字节流与字符流的区别

字节流和字符流使用是非常相似的,那么除了操作代码的不同之外,还有哪些不同呢?

字节流在操作的时候本身是不会用到缓冲区(内存)的,是与文件本身直接操作的,而字符流在操作的时候是使用到缓冲区的

字节流在操作文件时,即使不关闭资源(close方法),文件也能输出,但是如果字符流不使用close方法的话,则不会输出任何内容,说明字符流用的是缓冲区,并且可以使用flush方法强制进行刷新缓冲区,这时才能在不close的情况下输出内容

那开发中究竟用字节流好还是用字符流好呢?

在所有的硬盘上保存文件或进行传输的时候都是以字节的方法进行的,包括图片也是按字节完成,而字符是只有在内存中才会形成的,所以使用字节的操作是最多的。

如果要java程序实现一个拷贝功能,应该选用字节流进行操作(可能拷贝的是图片),并且采用边读边写的方式(节省内存)。

具体使用根据业务选择

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值