File Handling in Java
File handling in Java is used to create, read, write, update, and delete files. Java provides several classes in the java.io and java.nio packages for performing file operations.
File handling is useful for storing data permanently instead of keeping it only in memory.
Common File Operations
- Create a file
- Write data to a file
- Read data from a file
- Append data
- Delete a file
1. Creating a File
The File class can be used to create and manage files.
import java.io.File; import java.io.IOException; class Main { public static void main(String[] args) { try { File file = new File("data.txt"); if (file.createNewFile()) { System.out.println("File created successfully"); } else { System.out.println("File already exists"); } } catch (IOException e) { System.out.println("An error occurred"); } } }
2. Writing to a File
The FileWriter class is commonly used to write text into a file.
import java.io.FileWriter; import java.io.IOException; class Main { public static void main(String[] args) { try { FileWriter writer = new FileWriter("data.txt"); writer.write("Hello, Java!"); writer.close(); System.out.println("Data written successfully"); } catch (IOException e) { System.out.println("An error occurred"); } } }
Output in data.txt
Hello, Java!
3. Reading a File
The FileReader or BufferedReader class can be used to read text from a file.
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; class Main { public static void main(String[] args) { try { BufferedReader reader = new BufferedReader(new FileReader("data.txt")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (IOException e) { System.out.println("An error occurred"); } } }
Output
Hello, Java!
4. Appending Data
Appending means adding new data to the existing contents of a file.
import java.io.FileWriter; import java.io.IOException; class Main { public static void main(String[] args) { try { FileWriter writer = new FileWriter("data.txt", true); writer.write("\nWelcome to Java."); writer.close(); System.out.println("Data appended successfully"); } catch (IOException e) { System.out.println("An error occurred"); } } }
The true parameter tells FileWriter to append data instead of replacing the existing content.
5. Deleting a File
The delete() method can be used to delete a file.
import java.io.File; class Main { public static void main(String[] args) { File file = new File("data.txt"); if (file.delete()) { System.out.println("File deleted successfully"); } else { System.out.println("File not found"); } } }
Common File Handling Classes
| Class | Purpose |
|---|---|
File | Creates and manages file information |
FileReader | Reads character data |
FileWriter | Writes character data |
BufferedReader | Efficiently reads text |
BufferedWriter | Efficiently writes text |
FileInputStream | Reads byte data |
FileOutputStream | Writes byte data |
Character Stream vs Byte Stream
Character Stream
Used mainly for text data.
Examples:
FileReader FileWriter
Byte Stream
Used for binary data such as images, audio, and video.
Examples:
FileInputStream FileOutputStream
Advantages of File Handling
- Stores data permanently.
- Allows data to be reused later.
- Helps manage large amounts of data.
- Supports reading and writing files.
- Useful for storing application data.
- Provides support for text and binary files.
Key Points
- File handling is mainly provided through
java.ioandjava.nio. Fileis used to manage file information and operations.FileWriterwrites character data.FileReaderreads character data.FileInputStreamandFileOutputStreamhandle byte data.- Exceptions such as
IOExceptionshould be handled during many file operations.
Conclusion
File handling allows Java programs to create, read, write, append, and delete files. It is important for storing information permanently and working with external data. Java provides several classes and packages that make file operations easier and more efficient.