有趣的地方

有趣的地方

IO流的基础详解

目录

【1】File类:

【2】引入:IO流:

【3】形象理解:IO流 当做一根 “管”:​编辑

【4】IO流的体系结构:​编辑

【5】案例:通过java程序完成文件的复制操作

功能分解1:文件 --》程序:FileReader

功能分解2:程序--》文件:FileWriter

功能分解3:利用FileReader,FileWriter文件复制

警告:不要用字符流去操作非文本文件

 利用try-catch-finally处理异常方式

【6】FileInputStream读取文件中内容

【1】读取文本文件:

【2】利用字节流读取非文本文件:(以图片为案例:)--》一个字节一个字节的读取:

【3】利用字节类型的缓冲数组:

【7】FileInputStream,FileOutputStream完成非文本文件的复制

【1】读入一个字节,写出一个字节:

【2】利用缓冲字节数组:

【8】缓冲字节流(处理流)-BufferedInputStream ,BufferedOutputStream

【1】读入一个字节,写出一个字节:

【2】利用缓冲字节数组:

【3】利用缓冲区:因为硬盘的访问次数是有限的,访问过多,就寿命到了。为了减少访问次数。程序中做了缓存区。

【9】比对非文本文件复制的三种方法的效率

【10】缓冲字符流(处理流)-BufferedReader,BufferedWriter完成文本文件的复制

【10】转换流-InputStreamReader,OutputStreamWriter

【1】转换流:作用:将字节流和字符流进行转换。【2】转换流  属于 字节流还是字符流?属于字符流

     InputStreamReader  :字节输入流 ---》字符的输入流    OutputStreamWriter  : 字符输出流 --》字节的输出流

【3】图解:

【4】将输入的字节流转换为输入的字符流,然后完成文件--》程序 :

【5】转换流-InputStreamReader,OutputStreamWriter实现文本文件的复制



【1】File类:

封装文件/目录的各种信息,对目录/文件进行操作,但是我们不可以获取到文件/目录中的内容。


【2】引入:IO流:

I/O : Input/Output的缩写,用于处理设备之间的数据的传输。


【3】形象理解:IO流 当做一根 “管”:

备注:判断一个流是输入的还是输出的,是以程序为基准,进入程序叫输入,出程序叫输出。

【4】IO流的体系结构:

【5】案例:通过java程序完成文件的复制操作

功能分解1:文件 --》程序:FileReader

一个字符一个字符的将文件中的内容读取到程序中了:

package com.msb.io01;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
 * @author : msb-zhaoss
 */
public class Test01 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //文件--》程序:
        //1.有一个文件:----》创建一个File类的对象
        File f = new File("d:\\Test.txt");
        //2.利用FileReader这个流,这个“管”怼到源文件上去   ---》创建一个FileReader的流的对象
        FileReader fr = new FileReader(f);
        //3.进行操作“吸”的动作  ---》读取动作
        /*下面的代码我们验证了:如果到了文件的结尾处,那么读取的内容为-1
        int n1 = fr.read();
        int n2 = fr.read();
        int n3 = fr.read();
        int n4 = fr.read();
        int n5 = fr.read();
        int n6 = fr.read();
        System.out.println(n1);
        System.out.println(n2);
        System.out.println(n3);
        System.out.println(n4);
        System.out.println(n5);
        System.out.println(n6);*/
        //方式1:
        /*int n = fr.read();
        while(n!=-1){
            System.out.println(n);
            n = fr.read();
        }*/
        //方式2:
        int n;
        while((n = fr.read())!=-1){
            System.out.println((char)n); //ASCII码转字符
        }
        //4.“管”不用了,就要关闭  ---》关闭流
        //流,数据库,网络资源,靠jvm本身没有办法帮我们关闭,此时必须程序员手动关闭:
        fr.close();
    }
}





 原文件内容:

多读的内容展示

想一次性读取五个字符,不够的话下次再读五个字符:

package com.msb.io01;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/**
 * @author : msb-zhaoss
 */
public class Test02 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //文件--》程序:
        //1.创建一个File类的对象
        File f = new File("d:\\Test.txt");
        //2.创建一个FileReader的流的对象
        FileReader fr = new FileReader(f);
        //3.读取动作
        //引入一个“快递员的小车”,这个“小车”一次拉5个快递:
        char[] ch = new char[5];//缓冲数组
        int len = fr.read(ch);//一次读取五个:返回值是这个数组中 的有效长度
        while(len!=-1){
            //System.out.println(len);
            //错误方式:因为最后一次读取时,可能会多读
            /*for (int i = 0 ;i < ch.length;i++){
                System.out.println(ch[i]);
            }*/
            //正确方式:
            /*for (int i = 0 ;i < len;i++){
                System.out.println(ch[i]);
            }*/
            //正确方式2:将数组转为String:
            String str = new String(ch,0,len);
            System.out.print(str);
            len = fr.read(ch);
        }
        //4.关闭流
        fr.close();
    }
}

功能分解2:程序--》文件:FileWriter

package com.msb.io01;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
 * @author : msb-zhaoss
 */
public class Test03 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //1.有个目标文件:
        File f = new File("d:\\demo.txt");
        //2.FileWriter管怼到文件上去:
        FileWriter fw = new FileWriter(f);
        //3.开始动作:输出动作:
        //一个字符一个字符的往外输出:
        String str = "hello你好";
        for (int i = 0 ;i < str.length();i++){
            fw.write(str.charAt(i));
        }
        //4.关闭流:
        fw.close();
    }
}

备注:
如果目标文件不存在的话,那么会自动创建此文件。
如果目标文件存在的话:
new FileWriter(f)   相当于对原文件进行覆盖操作。
new FileWriter(f,false)  相当于对源文件进行覆盖操作。不是追加。
 new FileWriter(f,true)   对原来的文件进行追加,而不是覆盖。

利用缓冲数组:向外输出(利用缓冲数组:)

package com.msb.io01;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
 * @author : msb-zhaoss
 */
public class Test03 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //1.有个目标文件:
        File f = new File("d:\\demo.txt");
        //2.FileWriter管怼到文件上去:
        FileWriter fw = new FileWriter(f,true);
        //3.开始动作:输出动作:
        //一个字符一个字符的往外输出:
        String str = "你好中国";
        char[] chars = str.toCharArray();
        fw.write(chars);
        //4.关闭流:
        fw.close();
    }
}

功能分解3:利用FileReader,FileWriter文件复制


package com.msb.io01;
import java.io.*;
/**
 * @author : msb-zhaoss
 */
public class Test04 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //1.有一个源文件
        File f1 = new File("d:\\Test.txt");
        //2.有一个目标文件:
        File f2 = new File("d:\\Demo.txt");
        //3.搞一个输入的管 怼到源文件上:
        FileReader fr = new FileReader(f1);
        //4.搞一个输出的管,怼到目标文件上:
        FileWriter fw = new FileWriter(f2);
        //5.开始动作:
        //方式1:一个字符一个字符的复制:
        /*int n = fr.read();
        while(n!=-1){
            fw.write(n);
            n = fr.read();
        }*/
        //方式2:利用缓冲字符数组:
        /*char[] ch = new char[5];
        int len = fr.read(ch);
        while(len!=-1){
            fw.write(ch,0,len);//将缓冲数组中有效长度写出
            len = fr.read(ch);
        }*/
        //方式3:利用缓冲字符数组,将数组转为String写出。
        char[] ch = new char[5];
        int len = fr.read(ch);
        while(len!=-1){
            String s = new String(ch,0,len);
            fw.write(s);
            len = fr.read(ch);
        }
        //6.关闭流:(关闭流的时候,倒着关闭,后用先关)
        fw.close();
        fr.close();
    }
}

警告:不要用字符流去操作非文本文件

文本文件:.txt   .java  .c  .cpp  ---》建议使用字符流操作
非文本文件:.jpg,  .mp3  ,   .mp4 , .doc  , .ppt  ---》建议使用字节流操作

 利用try-catch-finally处理异常方式


package com.msb.io01;
import java.io.*;
/**
 * @author : msb-zhaoss
 */
public class Test04 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args)  {
        //1.有一个源文件
        File f1 = new File("d:\\Test.txt");
        //2.有一个目标文件:
        File f2 = new File("d:\\Demo.txt");
        //3.搞一个输入的管 怼到源文件上:
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader(f1);
            //4.搞一个输出的管,怼到目标文件上:
            fw = new FileWriter(f2);
            //5.开始动作:
            char[] ch = new char[5];
            int len = fr.read(ch);
            while(len!=-1){
                String s = new String(ch,0,len);
                fw.write(s);
                len = fr.read(ch);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //6.关闭流:(关闭流的时候,倒着关闭,后用先关)
            try {
                if(fw!=null){//防止空指针异常
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fr!=null){
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

【6】FileInputStream读取文件中内容

【1】读取文本文件:

package com.msb.io02;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
 * @author : msb-zhaoss
 */
public class Test01 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //功能:利用字节流将文件中内容读到程序中来:
        //1.有一个源文件:
        File f = new File("D:\\Test.txt");
        //2.将一个字节流这个管 怼  到 源文件上:
        FileInputStream fis = new FileInputStream(f);
        //3.开始读取动作
        /*
        细节1:
        文件是utf-8进行存储的,所以英文字符 底层实际占用1个字节
        但是中文字符,底层实际占用3个字节。如果用字节流读文件会将一个中文汉字读取三次,拆成三    
        个字节,不好知道,哪几个字节是那个字符
        细节2:
        如果文件是文本文件,那么就不要使用字节流读取了,建议使用字符流。
        细节3:
        read()读取一个字节,但是你有没有发现返回值是 int类型,而不是byte类型?
        read方法底层做了处理,让返回的数据都是“正数”
        就是为了避免如果字节返回的是-1的话,那到底是读入的字节,还是到文件结尾呢。
         */
        int n = fis.read();
        while(n!=-1){
            System.out.println(n);
            n = fis.read();
        }
        //4.关闭流:
        fis.close();
    }
}

【2】利用字节流读取非文本文件:(以图片为案例:)--》一个字节一个字节的读取:

package com.msb.io02;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
 * @author : msb-zhaoss
 */
public class Test02 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //功能:利用字节流将文件中内容读到程序中来:
        //1.有一个源文件:
        File f = new File("D:\\LOL.jpg");
        //2.将一个字节流这个管 怼  到 源文件上:
        FileInputStream fis = new FileInputStream(f);
        //3.开始读取动作
        int count = 0;//定义一个计数器,用来计读入的字节的个数
        int n = fis.read();
        while(n!=-1){
            count++;
            System.out.println(n);
            n = fis.read();
        }
        System.out.println("count="+count);
        //4.关闭流:
        fis.close();
    }
}

【3】利用字节类型的缓冲数组:

package com.msb.io02;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
 * @author : msb-zhaoss
 */
public class Test03 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //功能:利用字节流将文件中内容读到程序中来:
        //1.有一个源文件:
        File f = new File("D:\\LOL.jpg");
        //2.将一个字节流这个管 怼  到 源文件上:
        FileInputStream fis = new FileInputStream(f);
        //3.开始读取动作
        //利用缓冲数组:(快递员的小车)
        byte[] b = new byte[1024*6];
        int len = fis.read(b);//len指的就是读取的数组中的有效长度
        while(len!=-1){
            //System.out.println(len);
            for(int i = 0;i<len;i++){
                System.out.println(b[i]);
            }
            len = fis.read(b);
        }
        //4.关闭流:
        fis.close();
    }
}

【7】FileInputStream,FileOutputStream完成非文本文件的复制

【1】读入一个字节,写出一个字节:

package com.msb.io02;
import java.io.*;
/**
 * @author : msb-zhaoss
 */
public class Test04 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //功能:完成图片的复制:
        //1.有一个源图片
        File f1 = new File("d:\\LOL.jpg");
        //2.有一个目标图片:
        File f2 = new File("d:\\LOL2.jpg");
        //3.有一个输入的管道 怼 到 源文件:
        FileInputStream fis = new FileInputStream(f1);
        //4.有一个输出的管道 怼到  目标文件上:
        FileOutputStream fos = new FileOutputStream(f2);
        //5.开始复制:(边读边写)
        int n = fis.read();
        while(n!=-1){
            fos.write(n);
            n = fis.read();
        }
        //6.关闭流:(倒着关闭流,先用后关)
        fos.close();
        fis.close();
    }
}

【2】利用缓冲字节数组:
 

package com.msb.io02;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * @author : msb-zhaoss
 */
public class Test05 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //功能:完成图片的复制:
        //1.有一个源图片
        File f1 = new File("d:\\LOL.jpg");
        //2.有一个目标图片:
        File f2 = new File("d:\\LOL2.jpg");
        //3.有一个输入的管道 怼 到 源文件:
        FileInputStream fis = new FileInputStream(f1);
        //4.有一个输出的管道 怼到  目标文件上:
        FileOutputStream fos = new FileOutputStream(f2);
        //5.开始复制:(边读边写)
        //利用缓冲数组:
        byte[] b = new byte[1024*8];
        int len = fis.read(b);
        while(len!=-1){
            fos.write(b,0,len);//将数组中,有效长度写进,防止多写自己
            len = fis.read(b);
        }
        //6.关闭流:(倒着关闭流,先用后关)
        fos.close();
        fis.close();
    }
}

【8】缓冲字节流(处理流)-BufferedInputStream ,BufferedOutputStream

【1】读入一个字节,写出一个字节:

【2】利用缓冲字节数组:

【3】利用缓冲区:因为硬盘的访问次数是有限的,访问过多,就寿命到了。为了减少访问次数。程序中做了缓存区。

成上面的效果,单纯的靠FileInputStream,FileOutputStream是不可以完成的,这个时候就需要功能的加强,
这个加强就需要引入新的管道(在FileInputStream,FileOutputStream外面再套一层流):BufferedInputStream ,BufferedOutputStream. ----->处理流

代码:

package com.msb.io02;
import java.io.*;
/**
 * @author : msb-zhaoss
 */
public class Test06 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //1.有一个源图片
        File f1 = new File("d:\\LOL.jpg");
        //2.有一个目标图片:
        File f2 = new File("d:\\LOL2.jpg");
        //3.有一个输入的管道 怼 到 源文件:
        FileInputStream fis = new FileInputStream(f1);
        //4.有一个输出的管道 怼到  目标文件上:
        FileOutputStream fos = new FileOutputStream(f2);
        //5.功能加强,在FileInputStream外面套一个管:BufferedInputStream:
        BufferedInputStream bis = new BufferedInputStream(fis);
        //6.功能加强,在FileOutputStream外面套一个管:BufferedOutputStream:
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //7.开始动作 :
        byte[] b = new byte[1024*6];
        int len = bis.read(b);
        while(len!=-1){
            bos.write(b,0,len);
           /* bos.flush(); 底层已经帮我们做了刷新缓冲区的操作,缓存区满了后会自动推到目标文件 
            中去,不用我们手动完成:底层调用flushBuffer()*/
            len = bis.read(b);
        }
        //8.关闭流:
        //倒着关:
        //如果处理流包裹着节点流的话,那么其实只要关闭高级流(处理流),那么里面的字节流也会随之被关闭。
        bos.close();
        bis.close();
        /*fos.close();
        fis.close();*/
    }
}

【9】比对非文本文件复制的三种方法的效率

package com.msb.io02;
import java.io.*;
/**
 * @author : msb-zhaoss
 */
public class Test06 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //1.有一个源图片
        File f1 = new File("d:\\LOL.jpg");
        //2.有一个目标图片:
        File f2 = new File("d:\\LOL2.jpg");
        //3.有一个输入的管道 怼 到 源文件:
        FileInputStream fis = new FileInputStream(f1);
        //4.有一个输出的管道 怼到  目标文件上:
        FileOutputStream fos = new FileOutputStream(f2);
        //5.功能加强,在FileInputStream外面套一个管:BufferedInputStream:
        BufferedInputStream bis = new BufferedInputStream(fis);
        //6.功能加强,在FileOutputStream外面套一个管:BufferedOutputStream:
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //7.开始动作 :
        long startTime = System.currentTimeMillis();
        byte[] b = new byte[1024];
        int len = bis.read(b);
        while(len!=-1){
            bos.write(b,0,len);
           /* bos.flush(); 底层已经帮我们做了刷新缓冲区的操作,不用我们手动完成:底层调用flushBuffer()*/
            len = bis.read(b);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("复制完成的时间为:"+(endTime-startTime));
        //8.关闭流:
        //倒着关:
        //如果处理流包裹着节点流的话,那么其实只要关闭高级流(处理流),那么里面的字节流也会随之被关闭。
        bos.close();
        bis.close();
        /*fos.close();
        fis.close();*/
    }
}

【10】缓冲字符流(处理流)-BufferedReader,BufferedWriter完成文本文件的复制


package com.msb.io02;
import java.io.*;
/**
 * @author : msb-zhaoss
 */
public class Test07 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //1.有一个源文件:
        File f1 = new File("d:\\Test.txt");
        //2.有一个目标文件:
        File f2 = new File("d:\\Demo.txt");
        //3.需要一个管 怼到 源文件:
        FileReader fr = new FileReader(f1);
        //4.需要一根管怼到目标文件:
        FileWriter fw = new FileWriter(f2);
        //5.套一根管在输入字符流外面:
        BufferedReader br = new BufferedReader(fr);
        //6.套一根管在输出字符流外面:
        BufferedWriter bw = new BufferedWriter(fw);
        //7.开始动作:
        //方式1:读取一个字符,输出一个字符:
        /*int n = br.read();
        while(n!=-1){
            bw.write(n);
            n = br.read();
        }*/
        //方式2:利用缓冲数组:
        /*char[] ch = new char[30];
        int len = br.read(ch);
        while(len!=-1){
            bw.write(ch,0,len);
            len = br.read(ch);
        }*/
        //方式3:读取String:
        String str = br.readLine();//每次读取文本文件中一行,返回字符串
        while(str!=null){
            bw.write(str);
            //在文本文件中应该再写出一个换行:
            bw.newLine();//新起一行
            str = br.readLine();
        }
        //8.关闭流
        bw.close();
        br.close();
    }
}

【10】转换流-InputStreamReader,OutputStreamWriter

【1】转换流:作用:将字节流和字符流进行转换。
【2】转换流  属于 字节流还是字符流?属于字符流

     InputStreamReader  :字节输入流 ---》字符的输入流
    OutputStreamWriter  : 字符输出流 --》字节的输出流

【3】图解:

【4】将输入的字节流转换为输入的字符流,然后完成文件--》程序 :

package com.msb.io03;
import java.io.*;
/**
 * @author : msb-zhaoss
 */
public class Test01 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //文件---》程序:
        //1.有一个源文件:
        File f = new File("d:\\Test.txt");
        //2.需要一个输入的字节流接触文件:
        FileInputStream fis = new FileInputStream(f);
        //3.加入一个转换流,将字节流转换为字符流:(转换流属于一个处理流)
        //将字节转换为字符的时候,需要指定一个编码,这个编码跟原文件本身的编码格式统一
        //如果编码格式不统一的话,那么在控制台上展示的效果就会出现乱码
        //InputStreamReader isr = new InputStreamReader(fis,"utf-8");
        //获取当前idea控制台,或者叫 程序本身的编码
        InputStreamReader isr = new InputStreamReader(fis);
        //4.开始动作,将文件中内容显示在控制台:
        char[] ch = new char[20];
        int len = isr.read(ch);
        while(len!=-1){
            //将缓冲数组转为字符串在控制台上打印出来
            System.out.print(new String(ch,0,len));
            len = isr.read(ch);
        }
        //5.关闭流:
        isr.close();
    }
}

【5】转换流-InputStreamReader,OutputStreamWriter实现文本文件的复制

package com.msb.io03;
import java.io.*;
/**
 * @author : msb-zhaoss
 */
public class Test02 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //1.有一个源文件
        File f1 = new File("d:\\Test.txt");
        //2.有一个目标文件:
        File f2 = new File("d:\\Demo.txt");
        //3.输入方向:
        FileInputStream fis = new FileInputStream(f1);
        InputStreamReader isr = new InputStreamReader(fis,"utf-8");
        //4.输出方向:
        FileOutputStream fos = new FileOutputStream(f2);
        OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
        //5.开始动作:
        char[] ch = new char[20];
        int len = isr.read(ch);
        while(len!=-1){
            osw.write(ch,0,len);
            len = isr.read(ch);
        }
        //6.关闭流:
        osw.close();
        isr.close();
    }
}

发表评论:

Powered By Z-BlogPHP 1.7.3

© 2018-2020 有趣的地方 粤ICP备18140861号-1 网站地图