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.



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:

MethodsTypeOverview
canRead()BooleanThe file is tested to see if it is readable or not.
canWrite()BooleanVerifies the writability of the file.
createNewFile()BooleanThis command creates a new file that is empty.
delete()BooleanRemoves a file from the system.
exists()BooleanExistence of the file is tested.
getName()StringThis method returns the file’s name.
getAbsolutePath()StringThe absolute pathname of the file is returned by this function.
length()LongIn bytes, returns the file size.
list()String[]All the files in the directory will be returned as an array.
mkdir()BooleanA 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: 

import java.io.File; public class Main{ public static void main(String[] args) { File mrx_File=new File("FirstFile.txt"); System.out.println("Question: Can I read my file? "); boolean ample_result=mrx_File.canRead(); System.out.println("Answer: "+ample_result); } }

Java files handling examples


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: 

import java.io.File;public class Main{ public static void main(String[] args) {File mrx_File=new File("FirstFile.txt");System.out.println("Question: Can I write to my file? "); boolean ample_result=mrx_File.canWrite(); System.out.println("Answer: "+ample_result); } }

Java Files CreateNewFile() Method

A new file can be generated into the system by using the createNewFile() method as shown in the example:

Example: 

import java.io.File; import java.io.IOException; public class Main{ public static void main(String[] args) {try{ File ample=new File("NewFile.txt"); if(ample.createNewFile()){ System.out.println( "A new file named "+ample.getName()+" is created"); } else{ System.out.println("File named "+ample.getName()+" is already created in the directory"); } } catch (IOException e){ System.out.println("There was an error in creating a new file"); e.printStackTrace(); } } }

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: 

import java.io.File;public class Main{ public static void main(String[] args) {File mrx=new File("NewFile.txt");System.out.println("Does my File "+mrx.getName()+" exists ?"); System.out.println("Answer: "+mrx.exists());} }

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: 

import java.io.File;public class Main{ public static void main(String[] args) {File mrx=new File("NewFile.txt");System.out.println("What does my file name ?"); System.out.println("Answer: "+mrx.getName());} }

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: 

import java.io.File;public class Main{ public static void main(String[] args) {File mrx=new File("NewFile.txt");System.out.println("What is the absolute path of my "+mrx.getName()+" ?"); System.out.println("Answer: "+mrx.getAbsolutePath());} }

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: 

import java.io.File;public class Main{ public static void main(String[] args) {File mrx=new File("NewFile.txt");System.out.println("What is the Length of my "+mrx.getName()+" ?"); System.out.println("Answer: "+mrx.length());} }

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: 

import java.io.File; import java.io.IOException;public class Main { public static void main(String[] args) { try { File mrx = new File("C:\\Users\\public");String [] files_detail=mrx.list();for (int ample=0;ample< files_detail.length;ample++){ System.out.println(files_detail[ample]); } } catch (Exception e){ System.out.println("An Error occured"); } } }

Java Files Mkdir() Method

The mkdir() method creates a folder at any location in your pc.

Example below shows how to implement it:

Example: 

import java.io.File; import java.io.IOException;public class Main { public static void main(String[] args) { try { File mrx = new File("Files_Holder");if(mrx.mkdir()){ System.out.println("A directory/Folder is successfully created in your computer having the name: "+mrx.getName()); } else{ System.out.println("The directly already exists"); } } catch (Exception e){ System.out.println("An Error occurred"); } } }

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: 

import java.io.File; // Import the File class from Java.io package public class Main{ public static void main(String[] args) {File ample=new File("NewFile.txt");if(ample.delete()){System.out.println(ample.getName()+" is successfully deleted from your system"); } else{ System.out.println("An error is generated during the removal of this file"); } } }

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *