What is File Handling?
File Handling in Java is the process of creating, reading, writing, updating, and deleting files stored on a storage device such as a hard disk, SSD, or external drive.
Normally, data stored in variables exists only while the program is running. Once the program terminates, that data is lost. File handling enables applications to store data permanently so that it can be accessed even after the program has ended.
Java provides file handling support mainly through the java.io package and the modern java.nio.file package.
Why is File Handling Important?
File handling is used to:
- Store application data permanently
- Save user information
- Generate reports
- Maintain logs
- Store configuration settings
- Transfer data between applications
- Read existing records from files
- Backup important information
Without file handling, applications would have to rely solely on memory, causing all data to be lost when the application closes.
File Class in Java
The File class is the foundation of file handling in Java.
A File object represents a file or directory path in the file system. It provides methods to create, delete, rename, and retrieve information about files and directories.
Creating a File object does not create the actual file on disk. It simply creates an object that represents the path.
Common Methods of the File Class
1. createNewFile()
Creates a new empty file in the specified location.
Return Type
boolean
Returns
-
trueif the file is created successfully. -
falseif the file already exists.
Exception
Throws IOException if an error occurs during file creation.
Purpose
Used when an application needs to create a new file before storing data.
2. exists()
Checks whether a file or directory exists.
Return Type
boolean
Returns
-
trueif the file exists. -
falseif the file does not exist.
Purpose
Used before performing operations such as reading, writing, or deleting a file.
3. delete()
Deletes a file or an empty directory.
Return Type
boolean
Returns
-
trueif deletion is successful. -
falseif deletion fails.
Purpose
Used to remove unnecessary files from the system.
4. getName()
Returns the name of the file or directory.
Return Type
String
Purpose
Used when displaying or processing file names.
5. getAbsolutePath()
Returns the complete path of a file.
Return Type
String
Purpose
Helps identify the exact location of a file in the system.
6. getCanonicalPath()
Returns the canonical path of a file.
Return Type
String
Purpose
Provides the unique and standardized file path after resolving relative references such as:
...
It removes ambiguities from the path.
7. length()
Returns the size of a file.
Return Type
long
Purpose
Used to determine file size in bytes.
8. canRead()
Checks whether the file has read permission.
Return Type
boolean
Returns
-
trueif readable. -
falseotherwise.
Purpose
Used before reading file contents.
9. canWrite()
Checks whether the file has write permission.
Return Type
boolean
Returns
-
trueif writable. -
falseotherwise.
Purpose
Used before writing data into a file.
10. canExecute()
Checks whether the file can be executed.
Return Type
boolean
Purpose
Commonly used for executable files and scripts.
11. isFile()
Checks whether the path represents a file.
Return Type
boolean
Returns
-
trueif it is a file. -
falseotherwise.
Purpose
Helps distinguish files from directories.
12. isDirectory()
Checks whether the path represents a directory.
Return Type
boolean
Returns
-
trueif it is a directory. -
falseotherwise.
Purpose
Used when working with folders.
13. mkdir()
Creates a single directory.
Return Type
boolean
Purpose
Used to create one folder.
14. mkdirs()
Creates multiple nested directories.
Return Type
boolean
Purpose
Creates all required parent directories automatically.
15. renameTo()
Renames a file or directory.
Return Type
boolean
Purpose
Used when changing file or folder names.
16. list()
Returns all files and folders inside a directory.
Return Type
String[]
Purpose
Used to retrieve directory contents.
17. listFiles()
Returns file objects representing all files and directories inside a folder.
Return Type
File[]