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.
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.
To read binary data from a file using FileInputStream, you need to follow these steps:
FileInputStream by passing the file path.read().Here's a simple example:
1import java.io.FileInputStream;2import java.io.IOException;34public class ReadBinaryData {5public static void main(String[] args) {6FileInputStream fis = null;7try {8fis = new FileInputStream("example.bin");9int data;10while ((data = fis.read()) != -1) {11System.out.print((char) data);12};13} catch (IOException e) {14e.printStackTrace();15} finally {16if (fis != null) {17try {18fis.close();19} catch (IOException e) {20e.printStackTrace();21}22}23}24}25}
This is a binary file.
FileInputStream object that reads from the file named "example.bin".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.FileInputStream after you're done with it to avoid resource leaks.IOExceptions to manage any errors that occur during file operations.To write binary data to a file using FileOutputStream, follow these steps:
FileOutputStream by passing the file path.write().Here's an example:
1import java.io.FileOutputStream;2import java.io.IOException;34public class WriteBinaryData {5public static void main(String[] args) {6FileOutputStream fos = null;7try {8fos = new FileOutputStream("output.bin");9String data = "This is a binary file.";10byte[] bytes = data.getBytes();11fos.write(bytes);12} catch (IOException e) {13e.printStackTrace();14} finally {15if (fos != null) {16try {17fos.close();18} catch (IOException e) {19e.printStackTrace();20}21}22}23}24}
File written successfully.
FileOutputStream object that writes to the file named "output.bin".getBytes() method.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).IOExceptions to manage any errors during file operations.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.
1import java.io.FileInputStream;2import java.io.FileOutputStream;3import java.io.IOException;45public class CopyFile {6public static void main(String[] args) {7FileInputStream fis = null;8FileOutputStream fos = null;9try {10fis = new FileInputStream("source.bin");11fos = new FileOutputStream("destination.bin");1213int data;14while ((data = fis.read()) != -1) {15fos.write(data);16}17System.out.println("File copied successfully.");18} catch (IOException e) {19e.printStackTrace();20} finally {21if (fis != null) {22try {23fis.close();24} catch (IOException e) {25e.printStackTrace();26}27}28if (fos != null) {29try {30fos.close();31} catch (IOException e) {32e.printStackTrace();33}34}35}36}37}
File copied successfully.
| Concept | Description |
|---|---|
| FileInputStream | Used for reading bytes from a file. |
| FileOutputStream | Used 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. |
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!