Chapter-6 (1)
Chapter-6 (1)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 1
0132130807
Introduction of File handling
• File handling in Java allows you to create, read, update, and
delete files on the file system.
• Java provides various classes and methods in the `java.io`
and `java.nio.file` packages to facilitate these operations.
• In programming languages, file handling typically involves
opening a file, performing read or write operations on the file,
and then closing the file once the operations are completed.
• This ensures that the data is properly saved and the file
resources are released.
• File handling is essential for tasks such as reading
configuration files, saving user data, logging information, and
processing large datasets
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 2
0132130807
Key Topics of File Handling
1. **File Class** - Represents a file or directory path in the file system.
2. **FileReader and FileWriter**- Used for reading from and writing to text files.
5. **FileInputStream and FileOutputStream**- Used for reading and writing binary files.
7. **NIO (New Input/Output)**- Introduces classes like `Path`, `Files` for advanced file operation.
File class
In Java, the File class is used to represent file and directory paths in the file system. This class is part of
the java.io package and provides methods to interact with files and directories. Here is a brief overview of
the File class in Java:
•Creation: You can create a File object by providing a file path or a directory path as a string:
File file = new File("path/to/file.txt");
•File Operations: The File class provides methods to perform various file operations such as checking if
a file exists, creating a new file, deleting a file, getting file information, etc.
•File file = new File("path/to/file.txt");
•// Check if the file exists
boolean fileExists = file.exists(); file.exists();
•Directory Operations: You can also use the File class to perform operations on directories such as
listing files in a directory, creating a new directory, deleting a directory, etc
File directory = new File("path/to/directory"); // List files in a directory
String[] filesInDirectory = directory.list();
•File Path Manipulation: The File class provides methods to manipulate file paths, such as getting the
parent directory, checking if a path is a directory, etc.
boolean isDirectory = file.isDirectory();
•The File class in Java is a versatile class that allows you to work with files and directories in a platform-
independent way. It is commonly used for file handling operations in Java applications.
Using the File Class java example
import java.io.File;
FileReader
•Reading from Files: FileReader is used to read character data from files. It reads
data from a file one character at a time.
Closing the Reader: It's important to close the FileReader once you're done with it
to release system resources.
FileWriter
•Writing to Files: FileWriter is used to write character data to files. It writes data to a
file one character at a time.
Flushing and Closing: It's important to flush and close the FileWriter to ensure that
all data is written to the file and to release system resources.
Using BufferedReader and BufferedWriter
For better performance when reading and writing text files, it's common to
use BufferedReader and BufferedWriter in combination
with FileReader and FileWriter:
Reading from a File using FileReader and BufferedReader
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.
0132130807