IO流【详解】

一、IO流

1.1 IO说明

Input 输入

Output 输出

流: 例如水流,流量,即流是指数据流动传输

IO流就是指数据的输入输出

  • 例如: 将磁盘中的小说.txt,读取到java代码中 ---> 输入

  • 例如: 从java代码中,写到磁盘中创建出文件,并向文件中写入内容 --> 输出

image-20240607102626689

1.2 IO体系

IO

字节流

字符流

InputStream-输入流的抽象父类

FileInputStream

OutputStream-输出流的抽象父类

FileOutputStream

Reader-字符输入流抽象父类

FileReader

Writer-字符输出流抽象父类

FileWriter

字节流: 专用于传输字节文件,例如图片,音视频等

字符流:专用于传输纯字符文件,txt


关于IO操作,不管什么流,只要是

  • 读取数据(输入),方法都是read()

  • 写出数据(输出).方法都是write()

  • 无论什么流,最后一定记得close()

二、字节流

字节流,主要用于对字节文件(二进制文件,诸如图像音频类)输入输出

InputStream是输入流抽象父类

  • 一般使用子类FileInputStream

OutputStream是输出流抽象父类

  • 一般使用子类FileOutputStream

2.1 FileInputStream

FileInputStream(字节输入流)主要是对二进制文件读取

演示代码,使用字节输入流读取字符文件了,目的是为了演示读取效果

public static void main(String[] args) throws IOException {
    // 创建字节输入流
    FileInputStream fis = new FileInputStream("a.txt");
​
    // 读取一个字节
    // int a = fis.read();
    // System.out.println(a );
    //
    // int b = fis.read();
    // System.out.println(b );
    //
    // int c = fis.read();
    // System.out.println(c );
    //
    // int d = fis.read();
    // System.out.println(d );
​
    // 改造成循环,简化读取步骤
    int i = -1;
    while((i = fis.read()) != -1) {
        System.out.println(i );
    }
​
    // 最后一定要关流
    fis.close();
}
// 改造成try-catch-finally

public static void main(String[] args)  {
    FileInputStream fis = null;
    try {
        fis = new FileInputStream("a.txt");
        int i;
        while ((i = fis.read(  )) != -1) {
            System.out.println(i );
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fis.close( );
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2.2 FileOutputStream

字节输出流,主要用于写出数据

public static void main(String[] args) {
    FileOutputStream fos = null;
    try{
        // 创建字节输出流
        // 1)目的地文件不存在,会自动创建
        // 2)运行一次写出数据,默认是将之前文件的内容覆盖
        // 3)想要每次写出数据时追加在文件后面,构造方法中设置true
       fos = new FileOutputStream("b.txt",true);
        // 写出数据
        fos.write(103);
    }catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
​
}

2.3 拷贝文件

拷贝一个图片到另外一个位置

边读边写

public static void main(String[] args) throws Exception {
​
    long begin = System.currentTimeMillis( );
​
    // 创建字节输入流
    FileInputStream fis = new FileInputStream("E:\\justdoit.jpg");
    // 创建字节输出流
    FileOutputStream fos = new FileOutputStream("E:\\就是干.jpg");
​
    int i;
    while((i = fis.read(  )) != -1) {
        fos.write(i);
    }
​
    fis.close();
    fos.close();
​
    long end = System.currentTimeMillis( );
    System.out.println("拷贝完成,耗时"+(end - begin) + "毫秒" );
}
// 使用try-catch-finally

问题: 将图片换成大一点的文件,例如10M左右的文件,再拷贝一遍,看时间....

很慢......

2.4 缓冲字节流(高效)

拷贝太慢,啥原因? 输入流的read和输出流的write都是一次读/写一个字节

10M文件,有1000万字节.....

解决方案就是,可以利用数组来完成一次读/写多个字节,利用这个思路,JDK中提供了高速读写数据的流,即缓冲字节流

  • BufferedInputStream

  • BufferedOutputStream

三、字符流

字符流专用于操作字符文件,例如txt文件

Reader是字符输入流抽象父类

  • FileReader

Writer是字符输出流的抽象父类

  • FileWriter

3.1 FilerReader

public static void main(String[] args) throws Exception {
    // 创建字符输入流
    FileReader fr = new FileReader("a.txt");
​
    int i;
    while ((i = fr.read()) != -1) {
        System.out.println((char)i);
    }
​
    fr.close();
}

3.2 FileWriter

public static void main(String[] args) throws Exception {
​
    // 创建字符输出流
    // FileWriter fw = new FileWriter("c.txt");
    FileWriter fw = new FileWriter("c.txt",true);
​
    fw.write('9');
    fw.write("9999");
​
    fw.close( );
​
}

3.3 拷贝小说

边读边写

public static void main(String[] args)  {
       long begin = System.currentTimeMillis( ); 
       FileWriter fw = null;
       FileReader fr= null;
       try {
           fr = new FileReader("三体.txt");
           fw = new FileWriter("三体2.txt");
           int i;
           while ((i = fr.read()) != -1) {
               fw.write(i);
           }
       }catch (Exception e){
           e.printStackTrace();
       } finally {
           try {
               fr.close();
               fw.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       long end = System.currentTimeMillis( );
       System.out.println("拷贝完成,耗时"+(end - begin) + "毫秒" );
    }

3.4 缓冲字符流

BufferedReader

BufferedWriter

内部数组8192

public class Demo7 {
    public static void main(String[] args) {
​
        long begin = System.currentTimeMillis( );
        FileWriter fw = null;
        FileReader fr = null;
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            fr = new FileReader("E:\\三体.txt");
            br = new BufferedReader(fr);
​
            fw = new FileWriter("E:\\三体2.txt");
            bw = new BufferedWriter(fw);
​
            // int i;
            // while ((i = br.read( )) != -1) {
            //     bw.write(i);
            // }
​
            /**
             * 缓冲字符流,可以读一行,写一行
             * 但是记得写出换行符号
             */
           String line;
           while ((line = br.readLine()) != null) {
               bw.write(line);
               bw.newLine();
           }
​
        } catch (Exception e) {
            e.printStackTrace( );
        } finally {
            try {
                br.close( );
                bw.close( );
            } catch (IOException e) {
                e.printStackTrace( );
            }
        }
        long end = System.currentTimeMillis( );
        System.out.println("拷贝完成,耗时" + (end - begin) + "毫秒");
​
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值