codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

48 / 65 topics
43Java Files44Java Create & Write Files45Java Read Files46Java Delete Files47Java I/O Streams48FileInputStream & FileOutputStream49BufferedReader & BufferedWriter
Tutorials/Java Programming/FileInputStream & FileOutputStream
☕Java Programming

FileInputStream & FileOutputStream

Updated 2026-05-12
15 min read

FileInputStream & FileOutputStream

In the previous section, we explored character-based streams for reading and writing text data. However, when dealing with binary data such as images, audio files, or any non-textual content, you need to use different classes designed specifically for handling binary data. In this tutorial, we will dive into FileInputStream and FileOutputStream, which are used for reading and writing binary data in Java.

Introduction

FileInputStream is a class that extends the InputStream class and is used to read bytes from a file. Similarly, FileOutputStream is a class that extends the OutputStream class and is used to write bytes to a file. These classes are essential for handling binary files because they allow you to read and write data without converting it into characters.

Reading Binary Data with FileInputStream

Basic Usage

To read binary data from a file using FileInputStream, you need to follow these steps:

  1. Create an instance of FileInputStream by passing the file path.
  2. Read bytes from the stream using methods like read().
  3. Close the stream to free up resources.

Here's a simple example:

ReadBinaryData.java
1import java.io.FileInputStream;
2import java.io.IOException;
3
4public class ReadBinaryData {
5 public static void main(String[] args) {
6 FileInputStream fis = null;
7 try {
8 fis = new FileInputStream("example.bin");
9 int data;
10 while ((data = fis.read()) != -1) {
11 System.out.print((char) data);
12 };
13 } catch (IOException e) {
14 e.printStackTrace();
15 } finally {
16 if (fis != null) {
17 try {
18 fis.close();
19 } catch (IOException e) {
20 e.printStackTrace();
21 }
22 }
23 }
24 }
25}
Output
This is a binary file.

Explanation

  • FileInputStream fis = new FileInputStream("example.bin"); — This line creates a FileInputStream object that reads from the file named "example.bin".
  • fis.read() — The read() method reads a single byte of data. It returns an integer value between 0 and 255, or -1 if the end of the stream is reached.
  • (char) data — Since we are reading binary data, we cast it to a character to print it. This is just for demonstration purposes; in real scenarios, you might process the bytes differently.

Common Mistakes

  • Forgetting to close the stream: Always ensure that you close the FileInputStream after you're done with it to avoid resource leaks.
  • Handling exceptions properly: Make sure to handle IOExceptions to manage any errors that occur during file operations.

Writing Binary Data with FileOutputStream

Basic Usage

To write binary data to a file using FileOutputStream, follow these steps:

  1. Create an instance of FileOutputStream by passing the file path.
  2. Write bytes to the stream using methods like write().
  3. Close the stream to ensure all data is written and resources are freed.

Here's an example:

WriteBinaryData.java
1import java.io.FileOutputStream;
2import java.io.IOException;
3
4public class WriteBinaryData {
5 public static void main(String[] args) {
6 FileOutputStream fos = null;
7 try {
8 fos = new FileOutputStream("output.bin");
9 String data = "This is a binary file.";
10 byte[] bytes = data.getBytes();
11 fos.write(bytes);
12 } catch (IOException e) {
13 e.printStackTrace();
14 } finally {
15 if (fos != null) {
16 try {
17 fos.close();
18 } catch (IOException e) {
19 e.printStackTrace();
20 }
21 }
22 }
23 }
24}
Output
File written successfully.

Explanation

  • FileOutputStream fos = new FileOutputStream("output.bin"); — This line creates a FileOutputStream object that writes to the file named "output.bin".
  • String data = "This is a binary file."; — We define a string that we want to write as binary data.
  • byte[] bytes = data.getBytes(); — Convert the string into a byte array using the getBytes() method.
  • fos.write(bytes); — Write the byte array to the file.

Common Mistakes

  • Overwriting files: By default, FileOutputStream will overwrite the existing file. If you want to append to the file instead, use the constructor that takes a boolean parameter: new FileOutputStream("output.bin", true).
  • Not handling exceptions: Ensure you handle IOExceptions to manage any errors during file operations.

Practical Example

Let's create a practical example where we read binary data from one file and write it to another. This is useful for tasks like copying files or processing binary data.

CopyFile.java
1import java.io.FileInputStream;
2import java.io.FileOutputStream;
3import java.io.IOException;
4
5public class CopyFile {
6 public static void main(String[] args) {
7 FileInputStream fis = null;
8 FileOutputStream fos = null;
9 try {
10 fis = new FileInputStream("source.bin");
11 fos = new FileOutputStream("destination.bin");
12
13 int data;
14 while ((data = fis.read()) != -1) {
15 fos.write(data);
16 }
17 System.out.println("File copied successfully.");
18 } catch (IOException e) {
19 e.printStackTrace();
20 } finally {
21 if (fis != null) {
22 try {
23 fis.close();
24 } catch (IOException e) {
25 e.printStackTrace();
26 }
27 }
28 if (fos != null) {
29 try {
30 fos.close();
31 } catch (IOException e) {
32 e.printStackTrace();
33 }
34 }
35 }
36 }
37}
Output
File copied successfully.

Explanation

  • Reading from "source.bin" and writing to "destination.bin": This example reads binary data from one file and writes it to another, effectively copying the file.

Summary

ConceptDescription
FileInputStreamUsed for reading bytes from a file.
FileOutputStreamUsed for writing bytes to a file.
read()Reads a single byte of data from the stream.
write(byte[])Writes a byte array to the stream.
close()Closes the stream and releases system resources.

What's Next?

In the next tutorial, we will explore BufferedReader and BufferedWriter, which are used for reading and writing text data more efficiently by buffering input and output operations. This will help you handle larger files and improve performance in your Java applications.

Stay tuned!


PreviousJava I/O StreamsNext BufferedReader & BufferedWriter

Recommended Gear

Java I/O StreamsBufferedReader & BufferedWriter