Java FileOutputStream

📂 Java FileOutputStream

FileOutputStream is a byte stream class used to write raw byte data to a file.
It is mainly used for binary files like images, videos, or any non-text files, but can also write text files.

  • Package: java.io

  • Superclass: OutputStream


✅ Syntax

FileOutputStream fos = new FileOutputStream("file_path");
fos.write(byteData); // Write byte or byte array
fos.close(); // Close the stream

🔹 1. Writing Bytes to a File

import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamExample {
public static void main(String[] args) {
try (FileOutputStream fos = new FileOutputStream(“example.txt”)) {
String data = “Hello, this is written using FileOutputStream!”;
fos.write(data.getBytes()); // Convert string to bytes
System.out.println(“Data written successfully.”);
} catch (IOException e) {
System.out.println(“Error writing file: “ + e.getMessage());
}
}
}

  • Converts string to bytes using getBytes().

  • Automatically overwrites the file if it already exists.


🔹 2. Appending Data to a File

By default, FileOutputStream overwrites the file.
To append, pass true as the second argument:

import java.io.FileOutputStream;
import java.io.IOException;
public class AppendFileExample {
public static void main(String[] args) {
try (FileOutputStream fos = new FileOutputStream(“example.txt”, true)) {
String data = “\nAppending new line to the file!”;
fos.write(data.getBytes());
System.out.println(“Data appended successfully.”);
} catch (IOException e) {
System.out.println(“Error writing file: “ + e.getMessage());
}
}
}


🔹 3. Writing Binary Data (Images, Audio, etc.)

import java.io.FileOutputStream;
import java.io.IOException;
public class BinaryFileWrite {
public static void main(String[] args) {
byte[] bytes = {65, 66, 67}; // A, B, C in ASCII

try (FileOutputStream fos = new FileOutputStream(“binary.dat”)) {
fos.write(bytes);
System.out.println(“Binary data written successfully.”);
} catch (IOException e) {
System.out.println(“Error writing binary file: “ + e.getMessage());
}
}
}

  • Useful for writing images, audio, videos, and other binary files.


🔹 4. Copy File Using FileInputStream & FileOutputStream

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFileExample {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream(“source.txt”);
FileOutputStream fos = new FileOutputStream(“destination.txt”)) {

byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != –1) {
fos.write(buffer, 0, bytesRead);
}

System.out.println(“File copied successfully.”);

} catch (IOException e) {
System.out.println(“Error copying file: “ + e.getMessage());
}
}
}

  • Reads source file bytes and writes them to destination file.

  • Efficient for large files using a buffer.


🔹 5. Important Methods

Method Description
write(int b) Writes one byte to file
write(byte[] b) Writes byte array to file
write(byte[] b, int off, int len) Writes subset of byte array
close() Closes the stream and releases resources
flush() Forces all data to be written immediately

✅ Best Practices

  1. Use try-with-resources to automatically close streams.

  2. Use buffers for large files (combine with BufferedOutputStream).

  3. Handle IOException properly.


Summary

  • FileOutputStream = byte stream for output

  • Can overwrite or append files

  • Works with binary and text files

  • Often used with FileInputStream to copy files

You may also like...