IO流-Java学习记录day23

IO流概述:
先了解一下编码方式问题:
因为常用的IDEA编码是UTF-8而中国的电脑操作系统一般用GBK编码方式,从磁盘上读取到内存时,由于编码方式不一致会导致乱码问题。
GBK 编码中,中文字符占 2 个字节,英文字符占 1 个字节;
UTF-8 编码中,中文字符占 3 个字节,英文字符占 1 个字节;

File类只能操作文件对象本身,不能读写文件对象的内容。
读写数据内容,应该使用IO流。
IO流是一个水流模型:IO理解成水管,把数据理解成水流。
IO流的分类:
按照流的方向分为:输入流,输出流。
(1)输出流:以内存为基准,把内存中的数据写出到磁盘文件或者网络介质中去的流称为输出流。
输出流的作用:写数据到文件,或者写数据发送给别人。
(2)输入流:以内存为基准,把磁盘文件中的数据或者网络中的数据读入到内存中去的流称为输入流。
输入流的作用:读取数据到内存。
按照流的内容分为: 字节流,字符流。
(1)字节流:流中的数据的最小单位是一个一个的字节,这个流就是字节流。
(2)字符流:流中的数据的最小单位是一个一个的字符,这个流就是字符流。

流大体分为四大类:
        字节输入流:以内存为基准,把磁盘文件中的数据或者网络中的数据以一个一个的字节的形式读入到内存中去的流称为字节输入流。
        字节输出流:以内存为基准,把内存中的数据以一个一个的字节写出到磁盘文件或者网络介质中去的流称为字节输出流。
        字符输入流:以内存为基准,把磁盘文件中的数据或者网络中的数据以一个一个的字符的形式读入到内存中去的流称为字符输入流。
        字符输出流:以内存为基准,把内存中的数据以一个一个的字符写出到磁盘文件或者网络介质中去的流称为字符输出流。
     字节流                                                    字符流
 字节输入流                字节输出流                    字符输入流           字符输出流
 InputStream             OutputStream                  Reader              Writer           (抽象类,只是类型)
 FileInputStream         FileOutputStream              FileReader          FileWriter       (实现类,真正使用的管道)
 BufferedInputStream     BufferedOutputStream          BufferedReader      BufferedWriter   (实现类,高级缓冲流)
                                                       InputStreamReader   OutputStreamWriter(转换流)
 ObjectInputStream       ObjectOutputStream(序列化)

本篇文章不会对每一个流都做详细的介绍,只会关注一些平时自身需要注意的点,如需要具体ApI操作方式可自行查阅相关资料。
注意事项;io流默认是覆盖式写数据,每次写数据都会清空文件之前的数据!( 方案:OutputStream fos = new FileOutputStream(“文件名称.txt” , true);//追加管道)

//解决字节输入流单字节处理弊病。
public class FileInputStreamDemo {
    public static void main(String[] args) throws Exception {
      /*  File srcFile = new File("src/xiaohuihui.txt");
        InputStream is = new FileInputStream(srcFile);
        // 1.创建一个字节数组的大小与文件的大小一模一样大
        byte[] buffer = new byte[(int) srcFile.length()];
        int len = is.read(buffer);
        System.out.println(len);
        System.out.println(srcFile.length());
        String rs = new String(buffer);
        System.out.println(rs);*/

        InputStream is = new FileInputStream("src/xiaohuihui.txt");
        // 直接把字节输入流中的数据全部读取到一个字节数组中去返回!
        byte[] buffer = is.readAllBytes();
        String rs = new String(buffer);
        System.out.println(rs);
    }
}
字节流与字节缓冲流等性能统计分析。
   缓冲字符输入流提高了性能,同时多了一个按照行读取文本文件内容的新方法
    public String readLine():每次读取一行数据,读完返回null
 缓冲字符输出流提高了写字符的性能,还多了一个换行的功能:
 public void newLine():直接换行!

    (1)使用低级字节流:一个一个字节的复制。
    (2)使用低级字节流:一个一个数组的复制。
    (3)使用缓冲字节流:一个一个字节的复制。
    (4)使用缓冲字节流:一个一个数组的复制。

   
    总结:
        缓冲流结合字节数组读取数据的性能是最高的!
 */
public class CopyTimer03 {
    public static void main(String[] args) {
        // copy01(); // 淘汰!
        copy02();
        copy03();
        copy04();
    }

    /**(4)使用缓冲字节流:一个一个数组的复制。 */
    public static void copy04(){
        long startTimer = System.currentTimeMillis();
        try(
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\bb\\字符输入流.wmv"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\视频.wmv"));
        ){
            // 定义一个变量存储一个字节
            int len = 0 ;
            byte[] buffer = new byte[1024];
            while((len = bis.read(buffer))!=-1){
                bos.write(buffer,0,len);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        long endTimer = System.currentTimeMillis();
        System.out.println("缓冲流一个一个字节数组复制:"+ (endTimer - startTimer) / 1000.0+"s");
    }


    /**(3)使用缓冲字节流:一个一个字节的复制。 */
    public static void copy03(){
        long startTimer = System.currentTimeMillis();
        try(
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\bb\\字符输入流.wmv"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\03视频.wmv"));
        ){
            // 定义一个变量存储一个字节
            int bt = 0;
            while((bt = bis.read())!= -1){
                bos.write(bt);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        long endTimer = System.currentTimeMillis();
        System.out.println("缓冲流一个一个字节复制:"+ (endTimer - startTimer) / 1000.0+"s");
    }

    /**(2)使用低级字节流:一个一个数组的复制。 */
    public static void copy02(){
        long startTimer = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream("D:\\bb\\字符输入流.wmv");
                OutputStream os = new FileOutputStream("D:\\02视频.wmv")
        ){
            // 定义一个变量存储一个字节
            int len = 0 ;
            byte[] buffer = new byte[1024];
            while((len = is.read(buffer))!=-1){
                os.write(buffer,0,len);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        long endTimer = System.currentTimeMillis();
        System.out.println("字节流一个一个字节数组复制:"+ (endTimer - startTimer) / 1000.0+"s");
    }


    /**(1)使用低级字节流:一个一个字节的复制。 */
    public static void copy01(){
        long startTimer = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream("D:\\bb\\字符输入流.wmv");
                OutputStream os = new FileOutputStream("D:\\01视频.wmv")
                ){
              // 定义一个变量存储一个字节
            int bt = 0;
            while((bt = is.read())!= -1){
                os.write(bt);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        long endTimer = System.currentTimeMillis();
        System.out.println("字节流一个一个字节复制:"+ (endTimer - startTimer) / 1000.0+"s");
    }
}

public class InputStreamReaderDemo {
    public static void main(String[] args) throws Exception {
        // 跟默认的字符输入流的功能一样,以当前默认代码编码把字节流转换成字符流。
//        Reader fr = new InputStreamReader(new FileInputStream("xiaohuihui.txt"));
        // 使用指定的编码把原始的字节流按照指定编码转换成字符流,这样就可以避免乱码!
        Reader fr = new InputStreamReader(new FileInputStream("D:\\xiaohuihui"),"GBK");
        BufferedReader br = new BufferedReader(fr);

        String line = null ;
        while((line = br.readLine())!=null){
            System.out.println(line);
        }
    }
}
public class PrintStreamDemo {
    public static void main(String[] args) throws Exception {
        ps.println(100); // 打印整数
        ps.println(false); //
        ps.println("爱你哟");
        ps.close();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值