All about File Handling in Java .
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>>
, andcin<<
, 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:
- Byte Oriented Streams
- 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:
- InputStream: Carries data from input devices to Java applications.
- 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:
- Reader: Carries data in the form of characters from input devices to Java applications.
- 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:
FileOutputStream
FileInputStream
FileWriter
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 :
Note : If the file khushiV.txt not present in E Drive then first it will create it and then write the txt.
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 :
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 :
- 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 :
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:
- 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 :
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.