Java IO details -- byte stream and character stream

Keywords: Java less network

1, Byte stream overview

Byte stream is divided into byte output stream and byte input stream

Byte stream directly operates on binary data; byte stream is generally used to operate on media files, such as pictures

2, Byte output stream

outputStream in java is an abstract class that controls byte output and inherits Closeable, FlushableAutoCloseable Interface, because it is an abstract class, we must use its subclass object instantiation when using it. Closeable Interface provides the close method, which is used to close the flow, Flushable The flush method is provided to clear the cache and write the data to the specified location.

outputstream controls the output of the stream, so its main method is write

public abstract void write(int b) output single byte data
 public void write(byte[] b) output the data of byte array
 public void write(byte[] b,int off,int len) outputs the data in the specified position of byte array

Now we use the subclass FileoutputStream of outputStream to output files

FileOutputStream(File file) 
FileOutputStream(File file, boolean append)  

The two construction methods of FileOutputStream are shown above. The second parameter of the second construction method indicates whether to overwrite or add data. true means to add and false means to overwrite.

public static void main(String[] args) throws IOException {
    //1. Create File object
    File file=new File("F:"+File.separator+"test.txt");
    //2. Instantiate the parent object by subclass
    OutputStream stream=new FileOutputStream(file);
    //3. writing files
    stream.write("xxx".getBytes());
    //4. closing flow
    stream.close();
}

Output data in four steps

Three. inputStream

inputStream in java is an abstract class that controls byte input and inherits CloseableAutoCloseable Interface, you will find that compared with outputStream, there is less Flushable interface, because a buffer is needed to store the data to be written when byte output, while a buffer is not needed to read data.

Because inputStream is an abstract class, we still use its subclass, which is still introduced here through FileInputStream

FileInputStream(File file) 

The main method of inputStream is read

int read()  //Read a byte and return - 1 if it is empty
int read(byte[] b)  //Read all bytes, return byte length, if empty, return - 1
int read(byte[] b, int off, int len)  //Read the specified byte, return the byte length, and - 1 if it is empty
int available()  //Returns the length of bytes that can be read

Actual display by code

public static void main(String[] args) throws IOException {
    File file=new File("F:"+File.separator+"test.txt");
    //1. Instantiate the parent object by subclass
    InputStream stream=new FileInputStream(file);
    //2. Set read container
    byte[] data=new byte[1024];
    //3. Read data
    stream.read(data);
    System.out.println(new String(data));
    //4. closing flow
    stream.close();
}

4, Character flow overview

Byte stream is in the form of byte array when output, but in most cases, using string can simplify the output operation more, so character stream appears after jdk1.1.

5, Character output stream (Writer)

Writer is an abstract class that controls character output. It inherits Closeable, Flushable, Appendable, AutoCloseable Interface. Compared with outputStream, there is an additional appendable interface. Appendable provides an append method for appending input.

Appendable append(char c)  
Appendable append(CharSequence csq) . 
Appendable append(CharSequence csq, int start, int end)  

The main method in Writer is also write

void write(char[] cbuf) 
abstract void write(char[] cbuf, int off, int len) 
void write(int c) 
void write(String str) 
void write(String str, int off, int len)  

The most frequently used method here is the void write(String str) method. We practice it through FileWriter, a subclass of Writer

public static void main(String[] args) throws IOException {
    //1. Create File object
    File file=new File("F:"+File.separator+"test.txt");
    //2. Instantiate the parent object by subclass
    Writer writer=new FileWriter(file);
    //3. write
    writer.write("yyy");
    //4. additional
    writer.append("xxx");
    //5. close down
    writer.close();
}

6, Character input stream (Reader)

Reader is an input stream class that implements character data. Reader inherits Closeable, AutoCloseable, Readable Interface, Reader is similar to InputStream, mainly reading data through read() method:

int read() 
int read(char[] cbuf) 
abstract int read(char[] cbuf, int off, int len) 
int read(CharBuffer target)  

We still operate through the FileReader subclass:

public static void main(String[] args) throws IOException {
    File file=new File("F:"+File.separator+"test.txt");
    Reader reader=new FileReader(file);
    char[] data=new char[1024];
    int len=reader.read(data);
    System.out.println(new String(data,0,len));
    reader.close();
}

7, Comparison and summary

The first four streams inherit AutoCloseable Interface, so we can also AutoCloseable From the dynamic close class, the way to close is to use the try catch method.

public static void main(String[] args) throws IOException {
    try{
        File file=new File("F:"+File.separator+"test.txt");
        Reader reader=new FileReader(file);
        char[] data=new char[1024];
        int len=reader.read(data);
        System.out.println(new String(data,0,len));
    }catch (Exception e){
    }
}

As for when to use which stream for operation, byte stream belongs to binary data stream, so it is more commonly used in network transmission, picture and other data processing. Byte stream is not convenient to process Chinese, so character stream is used to process Chinese or string.

75 original articles published, 998 praised, 130000 visitors+
Private letter follow

Posted by ashton321 on Sat, 14 Mar 2020 06:18:19 -0700