All about File Handling in Java .

Khushi Vashishtha
CodeX
Published in
4 min readJul 19, 2024

File handling is an essential aspect of Java programming, allowing developers to perform input and output operations on files. Unlike C and C++ which use functions like printf(), scanf(), cout>>, and cin<<, Java utilizes streams to handle these operations. This guide will explore the concept of streams in Java, their types, and how to perform file operations using these streams.

What are Streams?

In Java, a stream is a channel or medium that facilitates the continuous flow of data between input devices and Java applications, and from Java applications to output devices. Java provides streams in the form of predefined classes in the java.io package. There are two main types of streams:

  1. Byte Oriented Streams
  2. Character Oriented Streams

Byte Oriented Streams

Byte oriented streams handle data in the form of bytes. Each data item in byte oriented streams is 1 byte long. There are two main types of byte oriented streams:

  1. InputStream: Carries data from input devices to Java applications.
  2. OutputStream: Carries data from Java applications to output devices.

Examples of InputStream:

  • ByteArrayInputStream
  • FileInputStream
  • BufferedInputStream
  • DataInputStream
  • ObjectInputStream

Examples of OutputStream:

  • ByteArrayOutputStream
  • FileOutputStream
  • BufferedOutputStream
  • PrintStream
  • DataOutputStream

Note: All byte oriented stream classes end with the word “Stream”.

Character Oriented Streams

Character oriented streams handle data in the form of characters. Each data item in character oriented streams is 2 bytes long. There are two main types of character oriented streams:

  1. Reader: Carries data in the form of characters from input devices to Java applications.
  2. Writer: Carries data in the form of characters from Java applications to output devices.

Examples of Reader:

  • CharArrayReader
  • FileReader
  • BufferedReader
  • InputStreamReader

Examples of Writer:

  • CharArrayWriter
  • FileWriter
  • PrintWriter
  • BufferedWriter

Note: All character oriented stream classes end either with “Reader” or “Writer”.

Performing File Operations

File operations often involve sending data to a target file or retrieving data from a source file. Java provides the following predefined classes for file operations:

  1. FileOutputStream
  2. FileInputStream
  3. FileWriter
  4. FileReader

1. How to Write Data from Files in Java using FileOutputStream :

The FileOutputStream class is used to send data from Java applications to a target file. Below is the Example of it :

Test.java

Note : If the file khushiV.txt not present in E Drive then first it will create it and then write the txt.

Output

2. How to Read Data from Files in Java using FileInputStream :

When working with file operations in Java, one of the most common tasks is reading data from a file. The FileInputStream class is a fundamental part of Java's I/O package, enabling you to read bytes from a file. Here's a step-by-step guide on how to use FileInputStream to get data from a file into a Java application. Below is the Example of it :

FileInput.java

Note : File must be present other it give Exception (java.io.FileNotFoundException: D:\KhushiVashishtha.txt (The system cannot find the file specified) .

3. How to Write Data from Files in Java using FileWriter :

FileWriter is a Java class used to write data to files in the form of characters. It is designed to handle the output of character streams. When you create a FileWriter object, it allows you to write content to a file character by character, making it suitable for writing text files. If the specified file does not exist, FileWriter will create a new file. If the file exists, it can either overwrite the existing content or append to it, based on the specified mode

Steps to Write Data Using FileWriter :

  1. Create a FileWriter Object
  • If the file does not exist, a new file is created.
  • If the file exists, a FileWriter object is created to write to the file.

2. Declare the Data and Convert it to a Character Array

3. Write Data to FileWriter

Complete Example :

Test.java

4. How to Read Data from Files in Java using FileWriter :

FileReader is a Java class used to read data from files in the form of characters. It is designed to handle the input of character streams. When you create a FileReader object, it allows you to read the content of a file character by character, making it suitable for reading text files.

Steps to Read Data Using FileReader:

  1. Create a FileReader Object :
  • If the file exists, a FileReader is created.
  • If the file does not exist, a java.io.FileNotFoundException is thrown.

2. Read Data from FileReader

· Read char by char in the form of its ASCII value.

· Convert char by char from ASCII value to character.

· Append char by char to a String variable.

Complete Example :

Test.java

Important Notes

  • Byte-Oriented vs. Character-Oriented Streams:
  • Byte-oriented streams (e.g., FileInputStream, FileOutputStream) can handle both byte-oriented data (e.g., images, documents) and character-oriented data.
  • Character-oriented streams (e.g., FileReader, FileWriter) can only handle character-oriented data.

--

--

Khushi Vashishtha
CodeX
Writer for

CS student | Java Developer | Tech Blogger @ Medium | Sharing Java insights, tutorials, and tips.