Several ways of reading and writing files in java.io

Recently, I was watching NIO, and by the way, I would like to summarize the use of IO.

1. InputStream, OutputStream (byte stream)

copy code
     // Read the file (byte stream) 
        InputStream in = new FileInputStream("d:\\1234.txt" );
         // Write the corresponding file 
        OutputStream out = new FileOutputStream("d:\\2134.txt" ) ;
         // Read data
         // How many bytes to take at one time 
        byte [] bytes = new  byte [1024 ];
         // Accept the read content (n represents the relevant data, just in the form of numbers) 
        int n = -1 ;
         // Loop out data 
        while ((n = in.read(bytes,0,bytes.length)) != -1 ) {
             // Convert to string 
            String str = new String(bytes,0,n,"GBK");
            System.out.println(str);
            // Write related files 
            out.write(bytes, 0 , n);
        }
        // Close the stream 
        in.close();
        out.close();
copy code

2. BufferedInputStream, BufferedOutputStream (cached byte stream) are used in the same way as byte stream, but more efficient (recommended)

copy code
    
        // Read the file (cache byte stream) 
        BufferedInputStream in = new BufferedInputStream( new FileInputStream("d:\\1234.txt" ));
         // Write the corresponding file 
        BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream("d :\\2134.txt" ));
         // Read data
         // How many bytes are taken at one time 
        byte [] bytes = new  byte [1024 ];
         // Accept the read content (n represents the relevant data, only But it is in the form of numbers) 
        int n = -1 ;
         // loop out data 
        while ((n = in.read(bytes,0,bytes.length)) != -1) {
             // Convert to string 
            String str = new String(bytes,0,n,"GBK" );
            System.out.println(str);
            // Write related files 
            out.write(bytes, 0 , n);
        }
        // Clear the cache 
        out.flush();
         // Close the stream 
        in.close();
        out.close();
copy code

Three, InputStreamReader, OutputStreamWriter (byte stream, this method is not recommended, can not directly read and write byte length). use range for character conversion

copy code
     //读取文件(字节流)
        InputStreamReader in = new InputStreamReader(new FileInputStream("d:\\1234.txt"),"GBK");
        //写入相应的文件
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("d:\\2134.txt"));
        //读取数据
        //循环取出数据
        byte[] bytes = new byte[1024];
        int len = -1;
        while ((len = in.read()) != -1) {
            System.out.println(len);
            //写入相关文件
            out.write(len);
        }
        //清楚缓存
        out.flush();
        //关闭流
        in.close();
        out.close();
copy code

 

四、BufferedReader、BufferedWriter(缓存流,提供readLine方法读取一行文本)

copy code
     //读取文件(字符流)
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\1234.txt"),"GBK"));
        //写入相应的文件
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\2134.txt"),"GBK"));
        //读取数据
        //循环取出数据
        String str = null;
        while ((str = in.readLine()) != null) {
            System.out.println(str);
            //写入相关文件
            out.write(str);
            out.newLine();
        }
        
        //清楚缓存
        out.flush();
        //关闭流
        in.close();
        out.close();
copy code

五、Reader、PrintWriter(PrintWriter这个很好用,在写数据的同事可以格式化)

copy code
     //读取文件(字节流)
        Reader in = new InputStreamReader(new FileInputStream("d:\\1234.txt"),"GBK");
        //写入相应的文件
        PrintWriter out = new PrintWriter(new FileWriter("d:\\2134.txt"));
        //读取数据
        //循环取出数据
        byte[] bytes = new byte[1024];
        int len = -1;
        while ((len = in.read()) != -1) {
            System.out.println(len);
            //写入相关文件
            out.write(len);
        }
        // Clear the cache 
        out.flush();
         // Close the stream 
        in.close();
        out.close();
copy code

Personal suggestion: read and write in the stream, it is recommended to use BufferedInputStream, BufferedOutputStream

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324845403&siteId=291194637