File Class in Java

File class only gives you access to the file and directory meta data.
If you need to read or write the content of files, you should do so using either FileInputStream/FileReader, FileOutputStream/FileWriter or RandomAccessFile.

Using File class you can do a lot of stuff such as mentioned below:

  • Check if a file or directory exists.
  • Create a file or directory if it does not exist.
  • Read the length of a file.
  • Rename or move a file.
  • Delete a file.
  • Check if path is file or directory.
  • Read list of files in a directory.

Constructors

File Class in Java

package com.java.io;

import java.io.File;
import java.io.IOException;

public class FileExample {
    public static void main(String[] args) {     
        File f = new File("/Test/TestFolder/");        //Print File attributes 
            
        System.out.println("File name :" + f.getName());     
        System.out.println("Path: " + f.getPath());     
        System.out.println("Absolute path:" + f.getAbsolutePath());     
        System.out.println("Parent:" + f.getParent());     
        System.out.println("Exists :" + f.exists());     
        if (f.exists())      {       
            System.out.println("Is writeable:" + f.canWrite());       
            System.out.println("Is readable" + f.canRead());       
            System.out.println("Is a directory:" + f.isDirectory());       
            System.out.println("File Size in bytes " + f.length());       
            System.out.println("Is executable:" + f.canExecute());    
        }     
        if (f.isDirectory()) {    
            String arr[] = f.list();             //find no. of entries in the directory 
                  
            int n = arr.length;          //displaying the entries 
                  
            for (int i = 0; i < n; i++) {                
                System.out.print(arr[i]);          //create File object with the entry and test 
                         //if it is a file or directory 
                        
                File f1 = new File(f.getAbsolutePath() + "/" + arr[i]);         
                if (f1.isFile()) {          
                    System.out.print(": is a file");         
                }        
                if (f1.isDirectory()) {          
                    System.out.print(": is a directory");         
                }        
                System.out.println("");      
            }       
            System.out.println("No of entries in this directory " + n);     
        }     //Create a new File if not exists.
            
        File createNewFile = new File("/Test/TestFolder/test-file100.txt");    
        try {
            if (createNewFile.createNewFile()) {  
                System.out.println("File is created!"); 
            } else {  
                System.out.println("File already exists."); 
            }
        } catch (IOException e) {
            e.printStackTrace();
        }  
    } 

}
File name :TestFolder
Path: /Test/TestFolder
Absolute path:/Test/TestFolder
Parent:/Test
Exists :true
Is writeable:true
Is readabletrue
Is a directory:true
File Size in bytes 448
Is executable:true
test-file.txt: is a file
test-file4.txt: is a file
test-file5.txt: is a file
test-file7.txt: is a file
test-file6.txt: is a file
test-file2.txt: is a file
test-file3.txt: is a file
test-file1.txt: is a file
test-file15.bin: is a file
test-file15.txt: is a file
test-file10.txt: is a file
No of entries in this directory 11
File is created!

Leave a Reply

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