File Output Stream In Java
Categories: Java 8(JDK1.8)
FileOutputStream is used to create a file and write data into it. The stream would create a file, if it doesn't already exist, before opening it for output.
Here are two constructors which can be used to create a FileOutputStream object.
Following constructor takes a file name as a string to create an input stream object to write the file −
OutputStream f = new FileOutputStream("C:/java/hello")
Following constructor takes a file object to create an output stream object to write the file. First, we create a file object using File() method as follows −
File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);
Once you have OutputStream object in hand, then there is a list of helper methods, which can be used to write to stream or to do other operations on the stream.
Sr.No.Method & Description
1. public void close() throws IOException{} - This method closes the file output stream. Releases any system resources associated with the file. Throws an IOException.
2. protected void finalize()throws IOException {} - This method cleans up the connection to the file. Ensures that the close method of this file output stream is called when there are no more references to this stream. Throws an IOException.
3. public void write(int w)throws IOException{} - This methods writes the specified byte to the output stream.
4. public void write(byte[] w) - Writes w.length bytes from the mentioned byte array to the OutputStream.