File handling constitutes a crucial aspect of any application.
Java provides various methods for creating, reading, updating, and deleting files.
The File class from the java.io package facilitates file manipulation.
To utilize the File class, instantiate an object of the class and specify the filename or directory name.
import
|
If you’re unfamiliar with the concept of a package, refer to our Java Packages Tutorial for clarification. |
The File class offers numerous helpful methods for file creation and retrieval of file information. For instance:
Method |
Type |
Description |
|
Boolean |
Determines whether the file is readable |
|
Boolean |
Checks whether the file is writable or not. |
|
Boolean |
Generate an empty file. |
|
Boolean |
Delete a file. |
|
Boolean |
Check if the file exists. |
|
String |
Retrieve the name of the file. |
|
String |
Retrieve the absolute pathname of the file. |
|
Long |
Retrieve the size of the file in bytes. |
|
String[] |
Retrieve an array containing files in the directory. |
|
Boolean |
Create a directory. |
In the upcoming chapters, you’ll discover how to create, write to, read from, and delete files.