Home >>Java Tutorial >Java Files

Java Files

Java Files

Java Files or the File class in Java language is basically an abstract representation of the file and the directory pathname.
A pathname in these files can be either absolute or relative. In Java language the file handling is known as an important part of any application.
There are numerous methods for deleting files creating, updating, and reading in Java.

Java File Handling

The java.io package consists of the file class and it permits the programmers to work with files. In order to use the File class the programmer have to create an object of the class and after that specify the filename or directory name.

Here is an example of the file handling in Java that will help you in understanding it better:

import java.io.File;  // Import the Java File class
File myObj = new File("filename.txt"); //Here Specify the filename

In order to create and retrieve the information about files, the File class has many useful methods that are depicted below in the form of a list along with a short description:

Method Type Description
canRead() Boolean This method id generally used to test whether the file is readable or not.
canWrite() Boolean This method id generally used to test whether the file is writable or not.
createNewFile() Boolean This method id generally used to create an empty file.
delete() Boolean This method id generally used to delete a file.
exists() Boolean This method id generally used to test test whether the file exists.
getName() String This method id generally used to return the name of the file.
getAbsolutePath() String This method id generally used to return the absolute pathname of the file.
length() Long This method id generally used to return the size of the file in bytes.
list() String[] This method id generally used to return an array of the files in the directory.
mkdir() Boolean This method id generally used to create a directory.