File handling is an important part of any application.
Java has several methods for creating, reading, updating, and deleting files.
Java File Handling
File class from the
java.io package, allows
us to work with files.
To use the
File class, create an object of
the class, and specify the filename or directory name:
import java.io.File; // Import the File class File myObj = new File("filename.txt"); // Specify the filename
If you don't know what a package is, read our Java Packages Tutorial.
File class has many useful methods for creating and getting information
||Boolean||Tests whether the file is readable or not|
||Boolean||Tests whether the file is writable or not|
||Boolean||Creates an empty file|
||Boolean||Deletes a file|
||Boolean||Tests whether the file exists|
||String||Returns the name of the file|
||String||Returns the absolute pathname of the file|
||Long||Returns the size of the file in bytes|
||String||Returns an array of the files in the directory|
||Boolean||Creates a directory|
You will learn how to create, write, read and delete files in the next chapters: