Java FileOutputStream Class

Category: Java   Tags: Java, Java File Handling, Java OutputStream Class, Java FileOutputStream Class

The FileOutputStream class creates an OutputStream and used to write bytes to a file. It has below declaration:

                                    public class FileOutputStream
                                    extends OutputStream
                                

Constructors

public FileOutputStream(File file) throws FileNotFoundException
Creates a file output stream to write to the File object file.
public FileOutputStream(String fileName) throws FileNotFoundException
Creates a file output stream to write to the file with the specified file name.
public FileOutputStream(String fileName, boolean append) throws FileNotFoundException
Creates a file output stream to write to the file with the specified file name. If append is true, then bytes will be written to the end of the file rather than the beginning.
public FileOutputStream(FileDescriptor fd) throws FileNotFoundException
Creates a file output stream by using the file descriptor fd, which represents an existing connection to an actual file in the file system.

Methods

public void write(int b) throws IOException
Writes the specified byte to this file output stream.
public void write(byte[] b) throws IOException
Writes b.length bytes from the specified byte array to this file output stream.
public void write(byte[] b, int offset, int numberOfBytes) throws IOException
Writes numberOfBytes bytes from the specified byte array starting at offset to this file output stream.
public void close() throws IOException
Closes this file output stream and releases any system resources associated with the stream. Further write attempts will generate an IOException.
protected void finalize() throws IOException
It ensures that the close method of this file output stream is called when there are no more references to this stream.
public final FileDescriptor getFD() throws IOException
Returns the file descriptor associated with this stream.

Here is a simple Java program for FileOutputStream:

FileOutputStreamExample.java

                            package com.tutorial.java.files;

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

                            public class FileOutputStreamExample {

                                public static void main(String[] args) throws FileNotFoundException, IOException {
                                    String str = "Write me off to the file.";
                                    FileOutputStream fout = null;
                                    try {
                                        fout = new FileOutputStream("/home/manish/sample.txt");
                                        fout.write(str.getBytes());
                                    } finally {
                                        if(fout != null)
                                            fout.close();
                                    }
                                }

                            }
                        

This Java program will write the given text into "sample.txt" file. Always close the output stream inside finally block so that it will always close even in case of any exception.