Java FileOutputStream
Writing data to files using byte streams
✏️ What is FileOutputStream?
FileOutputStream is a Java class used to write raw bytes to files. It creates new files or overwrites existing ones, perfect for saving binary data efficiently.
// Basic FileOutputStream example
import java.io.*;
FileOutputStream fos = new FileOutputStream("output.txt");
fos.write(65); // Writes 'A'
fos.close();
Result:
Creates 'output.txt' with content 'A'
FileOutputStream Features
File Writing
Write bytes to any file
FileOutputStream fos = new FileOutputStream("data.txt");
Overwrite Mode
Replace existing file content
fos.write("New content".getBytes());
Append Mode
Add to existing file content
new FileOutputStream("file.txt", true);
Binary Data
Save images, files, etc.
byte[] data = {65, 66, 67};
🔹 Basic FileOutputStream Usage
Here's how to write to a file using FileOutputStream:
import java.io.*;
public class FileOutputExample {
public static void main(String[] args) {
try {
// Create FileOutputStream
FileOutputStream fos = new FileOutputStream("message.txt");
// Write string as bytes
String message = "Hello, FileOutputStream!";
byte[] data = message.getBytes();
fos.write(data);
// Close the stream
fos.close();
System.out.println("File written successfully!");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
File written successfully!
File 'message.txt' contains: Hello, FileOutputStream!
🔹 Append Mode
Add content to existing files without overwriting:
import java.io.*;
public class AppendExample {
public static void main(String[] args) {
try {
// Create FileOutputStream in append mode
FileOutputStream fos = new FileOutputStream("log.txt", true);
// Add new line to existing file
String newEntry = "\nNew log entry: " + System.currentTimeMillis();
fos.write(newEntry.getBytes());
fos.close();
System.out.println("Log entry added!");
} catch (IOException e) {
System.out.println("Error writing log: " + e.getMessage());
}
}
}
Output:
Log entry added!
Content appended to 'log.txt'
🔹 Writing Binary Data
FileOutputStream can handle any type of binary data:
import java.io.*;
public class BinaryDataExample {
public static void main(String[] args) {
try (FileOutputStream fos = new FileOutputStream("binary.dat")) {
// Write different types of data
byte[] numbers = {10, 20, 30, 40, 50};
fos.write(numbers);
// Write single bytes
fos.write(65); // 'A'
fos.write(66); // 'B'
fos.write(67); // 'C'
System.out.println("Binary data written successfully!");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Binary data written successfully!
File 'binary.dat' contains: [10,20,30,40,50,65,66,67]
🔹 Copy File Example
Combine FileInputStream and FileOutputStream to copy files:
import java.io.*;
public class FileCopyExample {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("source.txt");
FileOutputStream fos = new FileOutputStream("copy.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("Copy error: " + e.getMessage());
}
}
}
Output:
File copied successfully!
'source.txt' copied to 'copy.txt'
🔹 Common Methods
Important FileOutputStream methods:
- write(int b) - Writes single byte
- write(byte[] b) - Writes byte array
- write(byte[] b, int off, int len) - Writes with offset
- flush() - Forces write of buffered data
- close() - Closes the stream
// Method examples
FileOutputStream fos = new FileOutputStream("example.txt");
fos.write(65); // Write single byte 'A'
fos.write("Hello".getBytes()); // Write string as bytes
fos.flush(); // Force write to disk
fos.close(); // Always close!