🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Java Notes
Topic #95

Java FileOutputStream


FileOutputStream

Earlier, you learned how to write text to files using FileWriter.

The FileOutputStream class works in a similar way, but it writes data as raw bytes. That means you can use it not only for text files, but also for binary files (like images, PDFs, or audio).


Write a Text File (Basic Example)

This example writes a short text string to a file using FileOutputStream.

Note: If the file already exists, its contents will be replaced (overwritten).

Example

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

public class Main {
  public static void main(String[] args) {
    // The text we want to write
    String text = "Hello World!";

    // try-with-resources: stream will be closed automatically
    try (FileOutputStream output = new FileOutputStream("filename.txt")) {
      output.write(text.getBytes());  // convert text to bytes and write
      System.out.println("Successfully wrote to file.");
    } catch (IOException e) {
      System.out.println("Error writing file.");
      e.printStackTrace();
    }
  }
}

Explanation: This program creates (or overwrites) filename.txt and writes Hello World! into it. If everything works, it prints "Successfully wrote to file." in the console. Otherwise, it shows an error message.


Copy a Binary File (Real-World Example)

The real strength of FileOutputStream is that it can handle any file type, not just text. Here is an example that copies an image file:

Example

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

public class CopyFile {
  public static void main(String[] args) {
    // Copy image.jpg into copy.jpg
    try (FileInputStream input = new FileInputStream("image.jpg");
         FileOutputStream output = new FileOutputStream("copy.jpg")) {

      int b;
      while ((b = input.read()) != -1) {
        output.write(b);  // write each raw byte to the new file
      }
      System.out.println("File copied successfully.");
    } catch (IOException e) {
      System.out.println("Error handling file.");
    }
  }
}

Explanation: This program reads image.jpg and writes it into copy.jpg. Since it works with raw bytes, it can copy any kind of file - text, images, audio, or PDFs.

Note: On the previous page, you saw FileInputStream for reading bytes. Here, it is paired with FileOutputStream to write bytes. Together, they make it possible to copy files.


Append to a File

By default, FileOutputStream overwrites the file if it already exists. To add (append) new content instead, pass true as the second argument:

Example

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

public class Main {
  public static void main(String[] args) {
    String text = "\nAppended text!";

    // true = append mode (keeps existing content)
    try (FileOutputStream output = new FileOutputStream("filename.txt", true)) {
      output.write(text.getBytes());
      System.out.println("Successfully appended to file.");
    } catch (IOException e) {
      System.out.println("Error writing file.");
      e.printStackTrace();
    }
  }
}

Explanation: This program adds Appended text! to the end of filename.txt, keeping the existing content.


Choosing the Right Class

Java gives you several ways to write to files. Here's when to pick each one:

  • FileWriter - best for basic text files. Simple and easy to use.
  • BufferedWriter - best for large text files, because it is faster and lets you add new lines easily.
  • FileOutputStream - best for binary data (images, PDFs, audio) or when you need full control of raw bytes.

Want to go beyond the notes?

Join CodingNow 2.0's Java course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Java FileOutputStream – FAQs

Quick answers about learning Java FileOutputStream in Java.

This free note from CodingNow 2.0 explains Java FileOutputStream in Java — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Java topic on CodingNow 2.0, including Java FileOutputStream, is 100% free with no signup required.
With focused practice, most students grasp Java FileOutputStream in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now