Java learning notes 40 (buffer stream)

Buffer stream:

Among the various streams of reading and writing files, the most troubling is efficiency,

The purpose of buffer stream is to improve the efficiency of reading and writing

 

Byte output buffer stream:

package demo;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

//Improve write efficiency
public class BufferedOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("d:\\buffer.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bos.write(66);
        byte[] bytes = "HelloWorld".getBytes();
        bos.write(bytes);
        bos.close();
    }
}

 

Byte input buffer stream:

package demo;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class BufferedInputStreamDemo {
    public static void main(String[] args) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\buffer.txt"));
        byte[] bytes = new byte[10];
        int len = 0;
        while ((len = bis.read(bytes)) != -1) {
            System.out.print(new String(bytes, 0, len));
        }
        bis.close();
    }
}

 

The buffer stream can be used to copy files, compared with the previous methods:

And compare the replication time

package demo;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Copy {
    public static void main(String[] args) throws IOException {
        long s = System.currentTimeMillis();
        copy1(new File("d:\\LOL.exe"), new File("e:\\LOL.exe"));
        long e = System.currentTimeMillis();
        System.out.println(e - s);// 14154 MS (14 seconds) copied

        copy2(new File("d:\\LOL.exe"), new File("e:\\LOL.exe"));
        // Same method test time: 129 MS (0.1 Second)

        copy2(new File("d:\\LOL.exe"), new File("e:\\LOL.exe"));
        // Test time: 94 MS (less than 0.1 Second)
    }

    public static void copy1(File src, File desc) throws IOException {
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(desc);
        int len = 0;
        while ((len = fis.read()) != -1) {
            fos.write(len);
        }
        fos.close();
        fis.close();
    }

    public static void copy2(File src, File desc) throws IOException {
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(desc);
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
        }
        fos.close();
        fis.close();
    }

    public static void copy3(File src, File desc) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
        int len = 0;
        byte[] bytes = new byte[1024 * 10];
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
        bos.close();
        bis.close();
    }
}

 

 

Character buffered output stream:

package demo;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterDemo{
    public static void main(String[] args) throws IOException {
        write();
    }
    public static void write() throws IOException{
        FileWriter fw = new FileWriter("d:\\buffer.txt");
        BufferedWriter bfw1 = new BufferedWriter(fw);
        bfw1.write(100);
        bfw1.flush();
        bfw1.write("Hello".toCharArray());
        bfw1.newLine();//Unique newline method
        //It can be used\r\n Newline, but this method is recommended and platform independent
        bfw1.flush();
        bfw1.write("HelloWorld");
        bfw1.flush();
        bfw1.close();        
    }
}

 

 

Character buffer input stream:

package demo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderDemo {
    public static void main(String[] args) throws IOException {
        read();
    }

    public static void read() throws IOException {
        int LineNumber = 0;
        BufferedReader bfr1 = new BufferedReader(new FileReader("d:\\read.txt"));
        // Buffer stream specific method, reading a single line of text
        String line = null;
        while ((line = bfr1.readLine()) != null) {
            LineNumber++;
            System.out.println("The first" + LineNumber + "Contents of line:" + line);
        }
        bfr1.close();
    }
}

 

Character buffer stream copy text file:

package demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Copy {
    public static void main(String[] args) throws IOException {
        BufferedReader bfr1 = new BufferedReader(new FileReader("d:\\read.txt"));
        BufferedWriter bfw1 = new BufferedWriter(new FileWriter("e:\\read.txt"));
        String line = null;
        while ((line = bfr1.readLine()) != null) {
            bfw1.write(line);
            bfw1.newLine();
            bfw1.flush();
        }
        bfw1.close();
        bfr1.close();
    }
}

 

 

Operation rules and selection of various flows:

1. Specify whether to read or write (source and destination)

2. What type of operation, byte or text?

3. Identify the device where the data is located, in hard disk, memory, or network? (memory streams and socket s are not covered here)

4. Do you need code conversion and use buffer stream and array to improve efficiency code?

Tags: Java less network socket

Posted on Mon, 04 May 2020 18:55:26 -0400 by drdapoo