JavaSE基础进阶知识笔记—IO流

系列文章目录

JavaSE中IO流的使用



前言

在这里插入图片描述


一、缓冲流

1.缓冲流概述

在这里插入图片描述
在这里插入图片描述

2.字节缓冲流

在这里插入图片描述
在这里插入图片描述

package org.example.buffer;

import java.io.*;

public class Demo {
    public static void main(String[] args) {

        try (

            //1.创建一个字节输入流管道与原视频接通
            FileInputStream is=new FileInputStream("C:\\Users\\***\\Pictures\\Saved Pictures\\1.png");

            //a.把原始字节输入流管道包装成高级的缓冲字节输入流
            InputStream bis=new BufferedInputStream(is);

            //2.创建一个字节输出流管道与目标文件接通
            FileOutputStream os=new FileOutputStream("file-io-app/src/b.png");

            //a.把原始字节输出流管道包装成高级的缓冲字节输出流
            OutputStream bos=new BufferedOutputStream(os);
            )
        {

            //3.定义一个字节数组转移数据
            byte[] buffer=new byte[1024];
            int len;
            while ((len=bis.read(buffer))!=-1){
                bos.write(buffer,0,len);
            }

            System.out.println("复制完成了");



        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3.字节缓冲流的性能分析

在这里插入图片描述

package org.example.buffer;

import java.io.*;

public class Demo2 {
    private static final String SRC_FILE="D:\\install\\Evernote_7.0.53.6060_station10.exe";
    private static final String DEST_FILE="D:\\";
    public static void main(String[] args) {
        copy01(); //使用低级字节流按照一个一个字节的形式复制文件  最慢
        copy02(); //使用低级字节流按照一个一个字节数组的形式复制文件    比较快,排第二
        copy03(); //缓冲流一个一个字节的形式复制文件  比较慢,排第三
        copy04(); //缓冲流一个一个字节数组的形式复制文件 最快 ,排第一
    }

    /**
     * 缓冲流一个一个字节数组的形式复制文件
     */
    private static void copy04() {
        long startTime=System.currentTimeMillis();
        try(
                //1.创建低级的字节输入流与源文件接通
                InputStream is=new FileInputStream(SRC_FILE);
                //a.把原始字节输入流管道包装成高级的缓冲字节输入流
                InputStream bis=new BufferedInputStream(is);
                //1.创建低级的字节输出流与目标文件接通
                OutputStream os=new FileOutputStream(DEST_FILE+"a4.exe");
                //b.把原始字节输出流管道包装成高级的缓冲字节输出流
                OutputStream bos=new BufferedOutputStream(os);
        ){
            //3.定义一个字节数组记录每次读取的字节(一个一个字节的复制)
            byte[] buffer=new byte[1024];
            int len;
            while ((len=bis.read(buffer))!=-1){
                bos.write(buffer,0,len);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        long endTime=System.currentTimeMillis();
        System.out.println("使用缓冲流字节数组的形式复制文件耗时:"+(endTime-startTime)/1000.0+"s");
    }


    /**
     * 缓冲流一个一个字节的形式复制文件
     */
    private static void copy03() {
        long startTime=System.currentTimeMillis();
        try(
                //1.创建低级的字节输入流与源文件接通
                InputStream is=new FileInputStream(SRC_FILE);
                //a.把原始字节输入流管道包装成高级的缓冲字节输入流
                InputStream bis=new BufferedInputStream(is);
                //1.创建低级的字节输出流与目标文件接通
                OutputStream os=new FileOutputStream(DEST_FILE+"a3.exe");
                //b.把原始字节输出流管道包装成高级的缓冲字节输出流
                OutputStream bos=new BufferedOutputStream(os);
        ){
            //3.定义一个变量记录每次读取的字节(一个一个字节的复制)
            int b;
            while ((b=bis.read())!=-1){
                bos.write(b);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        long endTime=System.currentTimeMillis();
        System.out.println("使用缓冲流一个一个字节复制文件耗时:"+(endTime-startTime)/1000.0+"s");
    }

    /**
     * 使用低级字节流按照一个一个字节数组的形式复制文件
     */
    private static void copy02() {
        long startTime=System.currentTimeMillis();
        try(
                //1.创建低级的字节输入流与源文件接通
                InputStream is=new FileInputStream(SRC_FILE);
                //1.创建低级的字节输出流与目标文件接通
                OutputStream os=new FileOutputStream(DEST_FILE+"a2.exe")
        ){
            //3.定义一个字节数组记录每次读取的字节(一个一个字节的复制)
            byte[] buffer=new byte[1024];
            int len;
            while ((len=is.read(buffer))!=-1){
                os.write(buffer,0,len);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        long endTime=System.currentTimeMillis();
        System.out.println("使用低级字节流按照一个一个字节数组的形式复制文件耗时:"+(endTime-startTime)/1000.0+"s");
    }

    /**
     * 使用低级字节流按照一个一个字节的形式复制文件
     */
    private static void copy01() {
        long startTime=System.currentTimeMillis();
        try(
                //1.创建低级的字节输入流与源文件接通
                InputStream is=new FileInputStream(SRC_FILE);
                //1.创建低级的字节输出流与目标文件接通
                OutputStream os=new FileOutputStream(DEST_FILE+"a1.exe")
                ){
            //3.定义一个变量记录每次读取的字节(一个一个字节的复制)
            int b;
            while ((b=is.read())!=-1){
                os.write(b);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        long endTime=System.currentTimeMillis();
        System.out.println("使用低级字节流按照一个一个字节的形式复制文件耗时:"+(endTime-startTime)/1000.0+"s");
    }
}

使用字节缓冲结合字节数组方式最快,其他不推荐使用。

4.字符缓冲流

在这里插入图片描述

在这里插入图片描述

package org.example.buffer;

import java.io.*;

public class Demo3 {
    public static void main(String[] args) {
        try (
                //1.创建一个文件字符输入流与源文件对接
                Reader rd =new FileReader("D:\\eclipse\\dropins\\a.txt");
                //a.将低级的字符输入流包装成高级的缓冲字符输入流
                BufferedReader br=new BufferedReader(rd);
                ){
            String line;
            while ((line=br.readLine())!=null){
                System.out.println(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

二、转换流

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

主要作用为解决乱码的问题

三、序列化和反序列化

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

四、打印流

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package org.example.printstream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * 目标:学会使用打印流 高效 方便写数据到文件中
 */
public class PrintStreamDemo1 {
    public static void main(String[] args) throws Exception {
        //1.创建一个打印流对象
        PrintStream ps=new PrintStream(new FileOutputStream("io-app/src/ps.txt",true));//在低级管道设置是否为追加数据
//        PrintStream ps=new PrintStream("io-app/src/ps.txt");
//        PrintWriter ps=new PrintWriter("io-app/src/ps.txt");//打印功能上与PrintStream没有区别

        ps.println(23);
        ps.println("我想打印啥就打印啥");
        ps.println('a');
        ps.close();
    }
}

package org.example.printstream;

import java.io.FileNotFoundException;
import java.io.PrintStream;

/**
 *  目标: 了解改变输出语句的位置到文件
 */
public class PrintDemo2 {
    public static void main(String[] args) throws Exception {
        System.out.println("23456");
        System.out.println("dafhljkg");

        //改变输出语句的位置
        PrintStream ps=new PrintStream("io-app/src/log.txt");
        System.setOut(ps);

        System.out.println("4rtyhj");
        System.out.println("sdfauopifljk");
    }
}

五、Properties

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package org.example.properties;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class PropertiesDemo1 {
    public static void main(String[] args) throws Exception {
        //需求:使用Properties把键值对信息存入到属性文件中去
        Properties properties=new Properties();
        properties.setProperty("孙悟空","大师兄");
        properties.setProperty("猪八戒","二师兄");
        properties.setProperty("沙和尚","小师弟");
        System.out.println(properties);

        /**
         * 参数一:输出管道
         * 参数二:注释内容
         */
        properties.store(new FileWriter("io-app/src/users.properties"),"给我一百");

        //加载属性文件到属性对象properties中
        Properties properties1=new Properties();
        properties1.load(new FileReader("io-app/src/users.properties"));
        System.out.println(properties1);

        String rs = properties1.getProperty("孙悟空");
        System.out.println(rs);
    }
}

六、IO框架

在这里插入图片描述
在这里插入图片描述


总结

主要讲解IO流相关的一些知识,包括了缓冲流,转换流,打印流,和IO框架的使用,还讲解了IO相关的一些操作,如序列化和反序列化,Properties文件的读写

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值