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 a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.