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.

FileOutputStream Constructors

Constructors Description
FileOutputStream(String name) Creates a file output stream to write to the file with the specified name.
FileOutputStream(String name, boolean append) Creates a file output stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.
FileOutputStream(File file) Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream(File file, boolean append) Creates a file output stream to write to the file represented by the specified File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.
FileOutputStream(FileDescriptor fdObj) Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.

FileOutputStream Methods

Method Description
void write(int b) This method used to write the specified byte to this file output stream.
void write(byte[] b) This method used to write b.length bytes from the specified byte array to this file output stream..
void write(byte[] b, int off, int len) This method used to write len bytes from the specified byte array starting at offset off to this file output stream.
FileChannel getChannel() This method used to return the file channel object associated with this file output stream.
FileDescriptor getFD() This method used to return the file descriptor associated with this stream.
void close() This method used to close the stream.

Program

 

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