Talk about two or three things about FileOutputStream file input stream

Guest officer, sit down on the small bench, let's start!

The FileOutputStream class inherits the OutputStream class, and the OutputStream class implements the Closeable and Flushable interfaces. FileInputStream In this chapter, we have already introduced the Closeable interface, so I won't say much here. What about the function of the Flushable interface? chat.

/**
 * @since 1.5
 */
public interface Flushable {

    /**
     * Flush the data in the buffer
     */
    void flush() throws IOException;
}

In fact, it is to write the stream data to the buffer.

However, note that this method is overridden by OutputStream:

public void flush() throws IOException {
}

OutputStream does nothing! The FileOutputStream class inherits the OutputStream class and does not override this method; therefore, there is no refresh function, which is just a decoration. In fact, it is used by other streams, such as the BufferedOutputStream class:

public synchronized void flush() throws IOException {
	flushBuffer();
	out.flush();
}

After understanding the flush() method, let's discuss the construction method of the FileOutputStream class!

In fact, the construction method of the FileOutputStream class is very similar to that of FileInputStream, except that there is one more parameter FileOutputStream(String name, boolean append) and the second parameter, which means append! (If you do not fill in the second parameter, the default is false)

public FileOutputStream(String name) throws FileNotFoundException {
	this(name != null ? new File(name) : null, false);
}
  • If boolean append is false, the file will be created if the file does not exist; if the file exists, the content will be overwritten and the original content will be deleted.
  • If, boolean append is true, the file is created if it does not exist; if the file exists, the content will be preserved and the data will be written in appending form.

In short, if the file does not exist, a new file will be created.

public class Test {
    public static void main(String[] args) {
        try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
            outputStream.write(65);
        } catch (Exception exception) {
            System.out.println("occur IO abnormal");
        }
    }
}
public class Test {
    public static void main(String[] args) {
        try (FileOutputStream outputStream = new FileOutputStream(filePath, true)) {
            outputStream.write(66);
        } catch (Exception exception) {
            System.out.println("occur IO abnormal");
        }
    }
}

The write() method at this time is really writing! Not in memory, but really written byte by byte to the file.

Well, let's talk about the most important write() method of the output stream: (there are also 3 overloaded methods)

Type ①:

public class Test {
    public static void main(String[] args) {
        try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
            outputStream.write(65);
        } catch (Exception exception) {
            System.out.println("occur IO abnormal");
        }
    }
}

public void write(int b) throws IOException{...}

The parameter is a byte to be written, why not use byte instead of int? exist FileInputStream This chapter has already been discussed, so I won't talk about it again.

There is no return value, once the writing is not successful, an IOException is thrown!

Type ②:

public class Test {
    public static void main(String[] args) {
        try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
            byte[] bytes = new byte[4];
            bytes[0] = 65;
            bytes[1] = 66;
            bytes[2] = 67;
            bytes[3] = 68;
            outputStream.write(bytes);
        } catch (Exception exception) {
            System.out.println("occur IO abnormal");
        }
    }
}

The argument is an array of bytes to be written, multiple bytes at a time.

The third kind:

public class Test {
    public static void main(String[] args) {
        try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
            byte[] bytes = new byte[4];
            bytes[0] = 65;
            bytes[1] = 66;
            bytes[2] = 67;
            bytes[3] = 68;
            outputStream.write(bytes, 2, 2);
        } catch (Exception exception) {
            System.out.println("occur IO abnormal");
        }
    }
}

1st parameter: an array of bytes to be written.

The second parameter: write from the first byte of this array, indicating the subscript index of the data.

3rd parameter: write a few bytes.

If either the range represented by the second parameter or the third parameter exceeds the range of the array, an exception will be thrown. Again, no return value.

Regarding the content related to the file descriptor, like FileInputStream, it will not be explained for the time being, and it is generally used less.

The above is all about FileOutputStream! I wonder if the guest official is enjoying it, let's chat in the comment area?

Tags: Java

Posted by amethyst42 on Wed, 04 May 2022 15:11:40 +0300