FileOutputStream in Java


File FileOutputStream JavaViews 2227

FileOutputStream class in Java

FileOutputStream is a class in Java that we use to write data into a file. We can either write byte-oriented or character-oriented data. The FileOutputStream class extends the OutputStream and we mainly use it to write primitive values. This is part of the java.io package. We use this class to write data that cannot be written as text such as PDF, image, excel, etc.

Hierarchy

FileOutputStream in Java

Ways to create a FileOutputStream

Below are the different ways to create a FileOutputStream in Java.

  • Using file path: We can specify the file path where we need to write the data. If the append parameter is true, it appends the new data, else it will overwrite the value.
    FileOutputStream fo = new FileOutputStream(file, append);
  • Using the file object: We can directly specify the file object where we need to write the information.
    FileOutputStream fo = new FileOutputStream(file);

Constructors of FileOutputStream

Below are the constructors of the Java FileOutputStream.

ConstructorDescription
FileOutputStream(File file)Creates a FileOutputStream to write to the specified file object
FileOutputStream(FileDescriptor fd)Creates a FileOutputStream to write data to the specified FileDescriptor object
FileOutputStream(String filename)Creates a FileOutputStream tp write data to the file specified with the string name
FileOutputStream(File file, boolean append)Creates a FileOutputStream to write data to the specified file object. If the append parameter is true, it will append the data, else it will overwrite the data.
FileOutputStream(String filename, boolean append)Creates a FileOutputStream to write data to the file with the specified file name. If the append parameter is true, it will append the data, else it will overwrite the data.

Java FileOutputStream Methods

MethodDescription
void close()Closes the specified file
void flush()Flushes the output stream by writing any buffered bytes of data.
FileChannel getChannel()Returns the unique FileChannel object associated with the output stream
FileDescriptor getFD()Returns the FileDescriptor associated with the stream
void write(byte[] b)Writes b.length of bytes to the specified output stream
void write(int b)Writes the specified byte data into the output stream
void write(byte[] b, int off, int len)Writes the len bytes of the byte array from the specified off position
OutputStream nullOutputStream()Returns a new OutputStream by discarding all the bytes.

Example: Write a single byte data using Java FileOutputStream

The below example shows how to write a single byte of data to the specified file. It converts the byte into a character while writing to the output stream. Hence the Output file will contain the data “K” which is the string equivalent of byte 75.

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

public class WriteFile {

  public static void main(String[] args) throws FileNotFoundException {
    FileOutputStream fo = new FileOutputStream("OutputFile.txt");
    byte b = 75;
    try {
      fo.write(b);
      fo.close();
      System.out.println("Single byte data successfully written to the file");
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

}
Single byte data successfully written to the file

Example: Write string data using Java FileOutputStream

We can also convert a string into a byte array and write the data into the specified file using the Java FileOutputStream class. Below is an example of writing a string into a file.

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

public class WriteFile {

  public static void main(String[] args) throws FileNotFoundException {
    FileOutputStream fo = new FileOutputStream("OutputFile.txt");
    String text = "Welcome to Java programming";
    try {
      byte[] b = text.getBytes();
      fo.write(b);
      
      fo.close();
      System.out.println("String data successfully written to the file");
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

}
String data successfully written to the file

Example: Append data using FileOutputStream

When we do not specify the append parameter while creating the FileOutputStream, by default it overwrites the data into the output file. All the above examples overwrite the new value. In the below example, we are appending the value by specifying the parameter as true. The output file initially contains the text “Welcome to Java programming.” Now it appends the new string to it and hence contains the information “Welcome to Java programming. This is an example of FileOutputStream.”

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

public class WriteFile {

  public static void main(String[] args) throws FileNotFoundException {
    FileOutputStream fo = new FileOutputStream("OutputFile.txt", true);
    String text = "This is an example of FileOutputStream";
    try {
      byte[] b = text.getBytes();
      fo.write(b);
      
      fo.close();
      System.out.println("Data is appended to the output file");
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

}
Data is appended to the output file

FileOutputStream in Java

Example: Write specific length bytes of data using FileOutputStream

In the below example, we write a specific length of bytes to the output file using the Java FileOutputStream. The offset position is 0 and length is 10 hence it writes 10 bytes of data. This means the output file contains the text “This is an”.

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

public class WriteFile {

  public static void main(String[] args) throws FileNotFoundException {
    FileOutputStream fo = new FileOutputStream("OutputFile.txt", true);
    String text = "This is an example of FileOutputStream";
    try {
      byte[] b = text.getBytes();
      fo.write(b, 0, 10);
      
      fo.close();
      System.out.println("Data is written to the output file");
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

}
Data is written to the output file

Example: FileOutputStream flush() method

The below example shows how to flush the bytes of information completely from the stream and write to the output stream.

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

public class FlushData {

  public static void main(String[] args) throws FileNotFoundException {
    FileOutputStream fo = new FileOutputStream("OutputFile.txt", false);
    String text = "Example of flush method";
    byte[] b = text.getBytes();
    try {
      fo.write(b);
      System.out.println("Successfully flushed bytes from the stream");
      fo.flush();
      fo.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

}
Successfully flushed bytes from the stream

 

Reference

 

Translate ยป