美文网首页
关于IO流操作的几种方式

关于IO流操作的几种方式

作者: 花开半時偏妍 | 来源:发表于2020-07-17 15:06 被阅读0次
    图片是转载的

    字节流和字符流的区别:
    读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。
    处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。
    结论:只要是处理纯文本数据,就优先考虑使用字符流。 除此之外都使用字节流。

    1.字节流,单个字节
    private static void method1() {
            // 普通字节流 /单个字节复制写入
            FileInputStream fi = null;
            FileOutputStream fo = null;
            try {
                fi = new FileInputStream("a.txt");
                fo = new FileOutputStream("b.txt");
                int n = 0;
                while ((n = fi.read()) != -1) {
                    fo.write(n);
                    fo.flush();
                }
                fi.close();
                fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
        }
    
    2.字节流,字节数组
    private static void method2() {
            // 普通字节流 /字节数组方式复制写入
            FileInputStream fi = null;
            FileOutputStream fo = null;
            try {
                fi = new FileInputStream("a.txt");
                fo = new FileOutputStream("b.txt");
                byte[] by = new byte[1024];
                int len = 0;
                while ((len = fi.read(by)) != -1) {
                    fo.write(by, 0, len);
                    fo.flush();
                }
                fi.close();
                fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
        }
    
    3.缓冲字节流,单个字节
    private static void method3() {
            // 高效字节流/单字节方式复制写入
            BufferedInputStream bi = null;
            BufferedOutputStream bo = null;
            try {
                bi = new BufferedInputStream(new FileInputStream("a.txt"));
                bo = new BufferedOutputStream(new FileOutputStream("b.txt"));
                int n = 0;
                while ((n = bi.read()) != -1) {
                    bo.write(n);
                    bo.flush();
                }
                bi.close();
                bo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    4.缓冲字节流,字节数组
    private static void method4() {
            // 高效字节流/字节数组方式复制写入
            BufferedInputStream bi = null;
            BufferedOutputStream bo = null;
            try {
                bo = new BufferedOutputStream(new FileOutputStream("b.txt"));
                bi = new BufferedInputStream(new FileInputStream("a.txt"));
                byte[] by = new byte[1024];
                int len = 0;
                while ((len = bi.read(by)) != -1) {
                    bo.write(by, 0, len);
                    bo.flush();
                }
                bi.close();
                bo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
        }
    
    5.字符流,单个字符
    private static void method5() {
            // 普通字符流/单字符方式复制写入
            FileReader fr = null;
            FileWriter fw = null;
            try {
                fr = new FileReader("a.txt");
                fw = new FileWriter("b.txt");
                int n = 0;
                while ((n = fr.read()) != -1) {
                    fw.write(n);
                    fw.flush();
                }
                fr.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
        }
    
    6.字符流,字符数组
    private static void method6() {
            // 普通字符流/字符数组方式复制写入
            FileReader fr = null;
            FileWriter fw = null;
            try {
                fr = new FileReader("a.txt");
                fw = new FileWriter("b.txt");
                char[] ch = new char[1024];
                int len = 0;
                while ((len = fr.read(ch)) != -1) {
                    fw.write(ch, 0, len);
                    fw.flush();
                }
                fr.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
        }
    
    7.缓冲字符流,单个字符
    private static void method7() {
            // 高效字符流/单字符方式复制写入
            BufferedReader br = null;
            BufferedWriter bw = null;
            try {
                br = new BufferedReader(new FileReader("a.txt"));
                bw = new BufferedWriter(new FileWriter("b.txt"));
                int n = 0;
                while ((n = br.read()) != -1) {
                    bw.write(n);
                    bw.flush();
                }
                bw.close();
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
        }
    
    8.缓冲字符流,字符数组
    private static void method8() {
            // 高效字符流/字符数组方式复制写入
            BufferedReader br = null;
            BufferedWriter bw = null;
            try {
                br = new BufferedReader(new FileReader("a.txt"));
                bw = new BufferedWriter(new FileWriter("b.txt"));
     
                char[] ch = new char[1024];
                int n = 0;
                while ((n = br.read(ch)) != -1) {
                    bw.write(ch, 0, n);
                    bw.flush();
                }
                bw.close();
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    9.缓冲字符流,按行读取
    private static void method9() {
            // 高效字符流/特殊方式复制写入
            BufferedReader br = null;
            BufferedWriter bw = null;
            try {
                br = new BufferedReader(new FileReader("a.txt"));
                bw = new BufferedWriter(new FileWriter("b.txt"));
                String s = null;
                while ((s = br.readLine()) != null) {
                    bw.write(s);
                    bw.newLine();
                    bw.flush();
                }
                br.close();
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
        }
    
    10.转换流

    InputStreamReader 字节流转字符流
    OutputStreamWriter 字符流转字节流

    10.1 字节转字符流并用缓冲流包装加快效率
    public static void readTest() throws IOException{
        InputStream in = System.in; //获取了标准的输入流。
    //      System.out.println("读到 的字符:"+ (char)in.read());  //read()一次只能读取一个字节。
            //需要把字节流转换成字符流。
            InputStreamReader inputStreamReader = new InputStreamReader(in);
            //使用字符流的缓冲类
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line = null;
            while((line = bufferedReader.readLine())!=null){
                System.out.println("内容:"+ line);
            }
        }
    

    实战示例:
    inputStreamReader将字节流转换为字符流进行读取,若要加快效率,可以用bufferReader包装类进行包装。
    outptStreamWriter将字符流转换为字节流进行输出,若要加快效率,可以用bufferWriter包装类进行包装。

    package com.app;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    
    public class A5 {
    
        public static void main(String[] args) {
            String filePath = "F:/123.txt" ;
            String filePath2 = "F:/abc.txt" ;
            File file = new File( filePath ) ;
            File file2 = new File( filePath2 ) ;
            copyFile( file , file2 );
    
        }
    
        private static void copyFile( File oldFile , File newFile ){
            InputStream inputStream = null ;
            InputStreamReader inputStreamReader = null ;
    
            OutputStream outputStream = null ;
            OutputStreamWriter outputStreamWriter = null ;
    
            try {
                inputStream = new FileInputStream( oldFile ) ; //创建输入流
                inputStreamReader = new InputStreamReader( inputStream ) ; //创建转换输入流
    
                outputStream = new FileOutputStream( newFile ) ; //创建输出流
                outputStreamWriter = new OutputStreamWriter( outputStream ) ; //创建转换输出流
    
                int result = 0 ;
    
                while( (result = inputStreamReader.read()) != -1){  //一次只读一个字符
                    outputStreamWriter.write( result ); //一次只写一个字符
                }
    
                outputStreamWriter.flush();  //强制把缓冲写入文件
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch (IOException e) {
                e.printStackTrace();
            }finally{
    
                if ( outputStreamWriter != null) {
                    try {
                        outputStreamWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                if ( inputStreamReader != null ) {
                    try {
                        inputStreamReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
        }
    }
    

    END,差不多基本够用了~

    相关文章

      网友评论

          本文标题:关于IO流操作的几种方式

          本文链接:https://www.haomeiwen.com/subject/iaeghktx.html