Files In Java
Detailed explanations and examples of Java files are provided, in order to better meet the learners’ needs.
Every application relies on file handling.
When it comes to Java Files, Java has several methods for creating, reading, updating, and deleting them.
- Files In Java:
- Java File Handling:
- Java Files CanRead() Method:
- Java Files CanWrite() Method:
- Java Files CreateNewFile() Method:
- Java Files Exist() Method:
- Java Files Getname() Method:
- Java Files GetAbsolutePath() Method:
- Java Files length() Method:
- Java Files list() Method:
- Java Files Mkdir() Method:
- Java Files Delete() Method:
Java File Handling
Using the File class from the java.io package, we can conduct different operations on files.
Java File class can be used by creating a class object, then specifying a filename or directory name:
Example
import java.io.File; public class Main { public static void main(String[] args) { File mrx_obj=new File("FirstFile.txt"); } }
import java.io.File; public class Main { public static void main(String[] args) { File mrx_obj=new File("SecondFile.txt"); } }
Our Java Packages Tutorial will teach you everything you need to know about packages if you are not familiar with them.
There are a variety of methods available in the File class that can be used for creating and retrieving information regarding files.
Mr Example:
Methods | Type | Overview |
canRead() | Boolean | The file is tested to see if it is readable or not. |
canWrite() | Boolean | Verifies the writability of the file. |
createNewFile() | Boolean | This command creates a new file that is empty. |
delete() | Boolean | Removes a file from the system. |
exists() | Boolean | Existence of the file is tested. |
getName() | String | This method returns the file’s name. |
getAbsolutePath() | String | The absolute pathname of the file is returned by this function. |
length() | Long | In bytes, returns the file size. |
list() | String[] | All the files in the directory will be returned as an array. |
mkdir() | Boolean | A directory is created. |
Java Files CanRead() Method
The canRead() method is implemented to check whether the file has the ability to be read or not.
For more understanding refer to the code below:
Example: 
Java Files CanWrite() Method
The canWrite() method is used in order to check the ability of the file to be written.
It returns true if the file can be written and vice versa.
Example: 
Java Files CreateNewFile() Method
A new file can be generated into the system by using the createNewFile() method as shown in the example:
Example: 
Java Files Exist() Method
To check if the file really exists in the system we use the exist() method.
This is done in cases when we have multiple files in our system. Show the example below:
Example: 
Java Files Getname() Method
The getName() method is used to obtain the name of the file.
The implementation of the method is shown in the example below:
Example: 
Java Files GetAbsolutePath() Method
This method getAbsolutePath() is responsible for returning the path or location of the file in the system which makes the process of finding the file easier.
Example: 
Java Files length() Method
The length() method returns the length of the file in bytes.
The given example shows the usage of the method more clearly:
Example: 
Java Files list() Method
list() method shows the list of all the .txt files in any directory.
You can understand its working by looking at the example below:
Example: 
Java Files Mkdir() Method
The mkdir() method creates a folder at any location in your pc.
Example below shows how to implement it:
Example: 
Java Files Delete() Method
delete() method is responsible for removing the specific file from the system.
The following example shows the working of the delete() method:
Example: