Java-FileOutputStream

FileOutputStream

FileOutputStream class possesses the functionality of writing one byte at a time into a file. In the following program, a data file named data.da is associated with an object of FileOutputStream class. Then, byte array by is written into the data file data.da. Finally, the file data.da is closed.

Program

hj

 

Binary File

Byte stream classes are used for dealing with data in binary format. A binary file is a computer file that is not a text file. Binary files typically contain bytes that are intended to be interpreted as something other than text characters. If a binary file is opened in a text editor, each group of eight bits will typically be translated as a single character, and the user will see a (probably unintelligible) display of textual characters.

Program Source

import java.io.FileOutputStream;
import java.io.IOException;

class Javaapp {
    
    public static void main(String[] args) {
        
        FileOutputStream fo = null;
        byte by[] = {20,40,60,80,100,120};
        
        try{
            fo = new FileOutputStream("data.da");
            fo.write(by);
            
        }catch(IOException io)
        {
            System.out.println("Exception : "+io);
            
        }finally{
            
            try{
                fo.close();
                
            }catch(IOException io)
            {
                System.out.println("Exception : "+io);
            }
        }
    }
}

Leave a Comment