Java File Class


The Java file class is an abstract that represents the file and the directory pathname.

 

The file class is used for creating new directories or files, file deletion, etc.


Constructors

S.No Constructor Description
1 File(File parent, String child) To create a new File instance from a parent abstract pathname and a child pathname string.
2 File(String pathname) To create a new File instance by converting the given pathname string into an abstract pathname.
3 File(String parent, String child) To create a new File instance from a parent pathname string and a child pathname string.
5 File(URI uri) To create a new File instance by converting the given file: URI into an abstract pathname.

Some Helpful Methods

S.No Method Description
1 public boolean isFile() This method is used to tests whether the file denoted by this abstract pathname is a normal file.
2 public boolean isDirectory() This method is used to tests whether the file denoted by this abstract pathname is a directory.
3 public String[] list() This method is used to returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
4 public boolean mkdir() It creates the directory named by this abstract pathname.
5 public String getName() This method is used to returns the name of the file or directory denoted by this abstract pathname.
6 public String getParent() It returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.
7 public String getPath() It converts this abstract pathname into a pathname string.
8 public boolean isAbsolute() It tests whether this abstract pathname is absolute
9 public boolean canRead() It tests whether the application can read the file denoted by this abstract pathname.
10 public boolean canWrite() It tests whether the application can modify to the file denoted by this abstract pathname.

Example:

import java.io.*;
public class FileDemo {  
	public static void main(String[] args) {  
	File f= null;
    	try {  
			f = new File("javaDemo.txt");  
            if (file.createNewFile()) {  
                System.out.println("New File is created Sucessfully");  
            }else {  
                System.out.println("File already exists.");  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
} 

Output

 New File is created Sucessfully



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.