Java FileOutputStream Class


In Java language, FileOutputStream is used to create a file and write data into a file.

 public class FileOutputStream extends OutputStream

FileOutputStream class methods

S.No Method Description
1 protected void finalize()throws IOException {} This method is used to cleans up the connection to the file.
2 public void write(byte[] a) It is used to Writes a.length bytes from the mentioned byte array to the file OutputStream.
3 public void write(int a) It is used to writes the specified byte to the file output stream.
4 public void close() This method is used to closes the file output stream.

Example:

import java.io.*;
public class FileOutputStreamDemo{
    public static void main(String args[]){  
        try {
			byte b [] = {13,1,32,30};
         	OutputStream out = new FileOutputStream("out.txt");
         	for(int i = 0; i < b.length ; i++) {
            	out.write( b[i] );   // writes the bytes
         	}
			System.out.println("Success"); 
        }catch(Exception e){
            System.out.println(e);
        }   
        finally {
                out.close();
        }
    }
} 

Output

Success 

out.txt

13
1
32
30 



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.