Java Programming Handbook

    FileInputStream and FileOutputStream

    In the previous blog, we covered the basics of InputStream and OutputStream classes. Now, let's dive into two essential classes that extend them: FileInputStream and FileOutputStream. These classes allow us to read from and write to files in Java.

    1. Understanding FileInputStream and FileOutputStream#

    • FileInputStream is used to read raw byte data from a file.
    • FileOutputStream is used to write raw byte data to a file.

    Since these classes deal with bytes, they are suitable for handling binary files such as images, audio, and videos. However, they can also process text files, though character streams (like FileReader and FileWriter) are generally preferred for that purpose.

    2. FileInputStream Class#

    The FileInputStream class allows reading data from a file as a stream of bytes. It extends InputStream and provides methods to read bytes from a file.

    ● Creating a FileInputStream#

    To create an instance of FileInputStream, you can pass the file path as a string or a File object:

    import java.io.*; public class FileInputStreamExample { public static void main(String[] args) { try (FileInputStream fis = new FileInputStream("example.txt")) { int data; while ((data = fis.read()) != -1) { System.out.print((char) data); // Reading byte by byte } } catch (IOException e) { e.printStackTrace(); } } }

    ● Important Methods in FileInputStream#

    • int read() - Reads one byte at a time and returns it.
    • int read(byte[] b) - Reads bytes into the given byte array.
    • int read(byte[] b, int off, int len) - Reads up to len bytes into an array starting at index off.
    • void close() - Closes the stream and releases file resources.
    • int available() - Returns the number of bytes available for reading.

    3. FileOutputStream Class#

    The FileOutputStream class allows writing byte data to a file. It extends OutputStream and provides methods to write data into a file.

    ● Creating a FileOutputStream#

    To write data into a file, we create an instance of FileOutputStream:

    import java.io.*; public class FileOutputStreamExample { public static void main(String[] args) { String data = "Hello, Java I/O!"; try (FileOutputStream fos = new FileOutputStream("output.txt")) { fos.write(data.getBytes()); System.out.println("Data written successfully."); } catch (IOException e) { e.printStackTrace(); } } }

    ● Important Methods in FileOutputStream#

    • void write(int b) - Writes a single byte.
    • void write(byte[] b) - Writes an array of bytes.
    • void write(byte[] b, int off, int len) - Writes len bytes starting at index off.
    • void close() - Closes the stream.
    • void flush() - Forces any buffered output to be written immediately.

    4. When to Use FileInputStream and FileOutputStream?#

    • Use FileInputStream when you need to read binary data (such as images, PDFs, etc.) or when working with raw bytes.
    • Use FileOutputStream when you need to write raw bytes to a file, like saving an image or a binary file.
    • If working with text files, consider using FileReader and FileWriter instead, as they handle character encoding properly.

    Conclusion#

    In this blog, we explored FileInputStream and FileOutputStream, understanding how they work and when to use them. We also covered practical examples of reading and writing files in Java.

    Next, we will cover ByteArrayInputStream and ByteArrayOutputStream, which allow working with data in memory instead of files.

    Last updated on Apr 09, 2025