We learned about the file byte input stream in the previous sections, so since there is a file byte input stream, there is also a corresponding file byte output stream. In this section we will learn about the file byte output stream.
Using output streams usually involves 4 basic steps:
In this section, you will become familiar with the above four basic steps by studying the file byte output stream.
If the file writing requirements are relatively simple, you can use the FileOutputStream class (file byte output stream), which is a subclass of the OutputStream class (write content to the file in bytes). The instance methods of this class are all from Inherited from the OutputStream class.
We can create an output stream pointing to a file using the following constructor method with refresh function of the FileOutputStream class.
FileOutputStream(Stringname);FileOutputStream(Filefile);
The first constructor uses the given file name to create a FileOutputStream stream; the second constructor uses a File object to create a FileOutputStream stream. The file specified by the parameters name and file is called the destination of the output stream.
The FileOutputStream output stream opens a channel to the file (the destination is this file, and the output stream points to this file).
Note : If the file pointed to by the output stream does not exist, Java will create the file. If the file pointed to by the output stream already exists, the output stream will refresh the file (so that the length of the file is 0).
In addition, the same as creating an input stream, errors (called exceptions) may occur when creating an output stream. For example, the file the output stream is trying to write may not allow the operation or has other restrictions. . Therefore, the output stream must be created in the try block part of the try-catch statement, and the exception must be detected and handled in the catch block part.
For example, create an output stream pointing to destination.txt:
try{FileOutputStreamout=newFileoutputStream(destin.txt);//Create an output stream pointing to the file destin.txt}catch(IOExceptione){System.out.println(Filewriteerror:+e);}
or
Filef=newFile(destin.txt);//Specify the destination of the output stream try{FileOutputStreamout=newFileOutputStream(f);//Create an output stream pointing to the destination}catch(IOExceptione){System.out.println(Filewrite:+ e);}
We can create an output stream pointing to a file using the following constructor method of the FileOutputStream class that can optionally have refresh functionality.
FileOutputStream(Stringname,booleanappend);FileOutputStream(Filefile,booleanappend);
When using the constructor method to create an output stream pointing to a file, if the append parameter is true, the output stream will not refresh the file pointed to (if the file already exists), and the write method of the output stream will start from the end of the file. Write data, the append parameter is false, and the output stream will refresh the pointed file (if the file already exists).