Java FileInputStream & FileOutputStream
In this page:
FileInputStream input = new FileInputStream("file_name");
FileOutputStream output = new FileOutputStream("file_name");
output.write(bytes);
Writing Bytes with FileOutputStream
FileOutputStream writes raw bytes to a file rather than characters, making it the right choice for binary data like images or serialized objects where no text encoding applies.
Example: Writing Bytes with FileOutputStream
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("data.bin");
out.write(65); // raw byte, not text
out.close();
System.out.println("Written");
}
}
Login to try C/C++/Java/PHP code in the editor
Reading Bytes with FileInputStream
FileInputStream reads raw bytes back from a file the same way, pairing with FileOutputStream for binary round-tripping — reading into a byte[] buffer rather than a String.
Example: Reading Bytes with FileInputStream
// Import java.io.FileOutputStream so it can be used by its short name
import java.io.FileOutputStream;
// Import java.io.FileInputStream so it can be used by its short name
import java.io.FileInputStream;
import java.io.IOException;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) throws IOException {
// Create a new FileOutputStream object and store it in out
FileOutputStream out = new FileOutputStream("data.bin");
out.write(65);
out.close();
// Create a new FileInputStream object and store it in in
FileInputStream in = new FileInputStream("data.bin");
// Print a line to the console
System.out.println(in.read());
in.close();
}
}
Login to try C/C++/Java/PHP code in the editor
Copying Binary Files
Copying a binary file means reading bytes from a FileInputStream in chunks and writing each chunk straight to a FileOutputStream, without ever interpreting the bytes as text.
Example: Copying Binary Files
// Import java.io.FileOutputStream so it can be used by its short name
import java.io.FileOutputStream;
// Import java.io.FileInputStream so it can be used by its short name
import java.io.FileInputStream;
import java.io.IOException;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) throws IOException {
// Create a new FileOutputStream object and store it in out
FileOutputStream out = new FileOutputStream("source.bin");
out.write(new byte[]{1, 2, 3});
out.close();
// Create a new FileInputStream object and store it in in
FileInputStream in = new FileInputStream("source.bin");
FileOutputStream copy = new FileOutputStream("copy.bin");
// Declare buffer and set it to new byte[1024]
byte[] buffer = new byte[1024];
int len;
// Repeat while (len = in.read(buffer)) != -1 is true
while ((len = in.read(buffer)) != -1) {
copy.write(buffer, 0, len);
}
in.close();
copy.close();
System.out.println("Copied");
}
}
Login to try C/C++/Java/PHP code in the editor
Auto-Close Try-With-Resources
Try-with-resources works the same way for byte streams as for character streams, automatically closing both the input and output streams once the copy operation completes or throws.
Example: Auto-Close Try-With-Resources
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
try (FileOutputStream out = new FileOutputStream("data.bin")) { // auto-closed
out.write(65);
}
System.out.println("Closed automatically");
}
}
Login to try C/C++/Java/PHP code in the editor
String to Bytes conversion
String.getBytes() converts text into raw bytes (using a specified or default charset) when you need to write string data out through a byte-oriented stream instead of a character-oriented one.
Example: String to Bytes conversion
// Import java.io.FileOutputStream so it can be used by its short name
import java.io.FileOutputStream;
// Import java.io.IOException so it can be used by its short name
import java.io.IOException;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) throws IOException {
// Declare bytes and set it to "Hello".getBytes()
byte[] bytes = "Hello".getBytes();
// Try code that might throw an exception
try (FileOutputStream out = new FileOutputStream("text.bin")) {
out.write(bytes);
}
// Print a line to the console
System.out.println("Written as bytes");
}
}
Login to try C/C++/Java/PHP code in the editor
- Using byte streams for text without specifying encoding.
- Reading a file one byte at a time without buffering, which is slow.
- Forgetting to close the streams.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: