This is the 8th day of my participation in Gwen Challenge

Accumulate over a long period, constant dripping wears away a stone 😄

I/O stream classification

  • According to the direction of data flow, it can be divided into input flow (reading) and output flow (writing).
    • Input stream: Reads data from hard disk into memory for use.
    • Output stream: Writes data in memory to disk for storage.
  • It is divided into byte stream and character stream according to different processing data units
    • Byte stream: A stream of read and write data in bytes.
    • Character stream: A stream that reads and writes data in character units.

1 character = 2 bytes, 1 byte = 8 bits, a Chinese character is two bytes long (GBK encoding), utF-8 encoding a Chinese character is three bytes.

  • Top parent class
The input stream The output stream
Byte stream InputStream OutputStream
Characters of the flow Reader Writer

Streams ending in Stream are byte streams, and streams ending in Writer or Reader are character streams.

All input streams are subclasses of either the abstract IuputStream (byte input stream) or the abstract Reader (character input stream).

All output streams are subclasses of the abstract OutputStream(byte OutputStream) or Writer(character OutputStream) class.

Character stream can achieve any function byte stream can achieve, not vice versa. Binary files such as pictures and videos can only be read and written using byte streams.

2, byte OutputStream

Java.io.OutputStream This abstract class is a superclass of all the classes that represent the output byte stream. Writes the specified byte information to the destination. It defines the basic public methods for byte output streams.

  • Void close() : Closes this output stream and frees any system resources associated with this stream.
  • Void flush() : Flushes this output stream and forces any buffered output bytes to be written out.
  • Void write(byte[] b) : writes b.length bytes from the specified byte array to this output stream.
  • Void write(byte[] b, int off, int len) : Writes len bytes from the specified byte array, starting from offset OFF to the output stream.
  • Abstract void write(int b) : Writes the specified byte to the output stream.

Let’s take a look at the subclasses of OutputStream. The ones listed below belong to the java.io package.

2.1 FileOutputStream

OutputStream has many subclasses, so we’ll start with the simplest one. The java.io.FileOutputStream class is a FileOutputStream used to write data in memory to a file on disk.

2.1.1 Construction method

We usually use these two:

  • FileOutputStream(File File) : Creates a File output stream that writes data to the File represented by the specified File object.
  • FileOutputStream(String name) : Creates an output file stream that writes data to a file with the specified name.

Parameter Meaning: The destination to which data is written

The function of the construction method:

  • 1. Create oneFileOutputStreamobject
  • An empty file will be created based on the file/file path passed by the constructor
  • 3, will takeFileOutputStreamThe object points to the created file

For example, the code is as follows:

public static void main(String[] args) {
     // Create a stream object with the File object
     File file = new File("a.txt"); 
     FileOutputStream fos = new FileOutputStream(file); 
     // Create a stream object with the file name
     FileOutputStream fos2 = new FileOutputStream("b.txt"); 
}
Copy the code

When you create a stream object, you must pass in a file path. If the file does not exist in this path, an empty file is created. If this file exists, it overwrites the previous file.

2.1.2 coding

  • Write bytes: write(int b) method
    // Create a FileOutputStream object whose constructor passes the write data destination
    // This destination uses a relative path
    FileOutputStream fos = new FileOutputStream("IO stream / / a.t xt");
    // Call the FileOutputStream method write to write data to the file
    fos.write(97);
    // Release resources
    fos.close();
Copy the code

I said 97 why is it an A when I open the file? This is because when the file is opened, it will query the encoding table, convert itself to character representation, 0-127 query ASCII table, the rest of the value query system default code table (GBK).

  • Write (byte[] b)
     public static void main(String[] args) throws IOException {
        // Create a FileOutputStream object whose constructor passes the write data destination
        // This destination uses a relative path
        FileOutputStream fos = new FileOutputStream("IO stream / / a.t xt");
        /** * write(byte[] b: writes B. length bytes from the specified byte array to the output stream of this file. * If the first byte is negative, then the first byte and the second byte form a Chinese display, query the system default encoding table GBK */

        / / A zong ji F
        byte[] c = {65, -66.67, -68, -69.70};
        fos.write(c);
        // Release resourcesfos.close(); }} Output result: A heald thistle FCopy the code
  • Write (byte[] b, int off, int len)
 public static void main(String[] args) throws IOException {
        // Create a FileOutputStream object whose constructor passes the write data destination
        // This destination uses a relative path
        FileOutputStream fos = new FileOutputStream("IO stream / / a.t xt");
        /** * write(byte[] b, int off, int len) Writes len bytes from the specified byte array starting with offset off to the file output stream. Len: Write a few bytes */
        //BCD
        byte[] b = "ABCDE".getBytes();
        fos.write(b,1.3);

        // Release resourcesfos.close(); } Output: BCDCopy the code

Additional writing

As demonstrated above, each time the program is run and the output stream object is created, the data in the target file is emptied. How do you keep the data in the target file and still add new data?

  • FileOutputStream(String Name, Boolean Append) Creates an output file stream that writes data to a file with the specified name.
  • FileOutputStream(File File, Boolean Append) Creates a File output stream that writes data to the File represented by the specified File object.

Append: append write switch

  • True: The created object does not overwrite the source file, and appending data to the end of the file
  • False: Create a new file and overwrite the source file
  public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("IO stream / / b.t xt".true);
        fos.write("Hello?".getBytes());
      * Windows :\r\n * Linux:/n * Max :/r */
        fos.write("\r\n".getBytes());
        fos.close();
    }
Copy the code

Start the application repeatedly

  • If you have any questions or errors in this article, please feel free to comment. If you find this article helpful, please like it and follow it.