1. A java file object represent just name of the file/directory.
     
    File f = new File("abc.txt");
    
  2. If ‘abc.txt’ is already available then ‘f’ will represent that physical file.
  3. If it is not already available, It won’t create any new file and ‘f’ simply represents the name of the file.
  4. Example:

     
    import java.io.File;
    import java.io.IOException;
    
    public class FileDemo {
    	public static void main(String[] args) {
    		try {
    			File file = new File("xyz.txt");
    			System.out.println(file.exists());
    			file.createNewFile();
    			System.out.println(file.exists());
    		} catch (IOException exception) {
    			exception.printStackTrace();
    		}
    	}
    }
    /*
    Output:
    Ist Run:
    false
    true
    IInd Run:
    true
    true
    */
    
  5. A java file Object can represent directories also.
  6. Example:

     
    import java.io.File;
    
    public class FileDemo {
    	public static void main(String[] args) {
    		File f = new File("bbc");
    		System.out.println(f.exists());
    		f.mkdir();
    		System.out.println(f.exists());
    	}
    }
    /*
    Output:
    Ist Run:
    false
    true
    IInd Run:
    true
    true
    */
    
The constructors of the file class
Constructor Description
File f = new File(String name) Here name may be file or directory name.
File f = new File(String subDir, String name) Creates a java file object that represents a file or directory name.
File f = new File(File subDir, String name) Creates a java file object that represents file or directory name present in specified sub directory.
Important methods of File Class
Method name Description
boolean exists() Returns true if the physical file/directory presents. other wise it returns false
boolean createNewFile() Returns ture if it creates a new file, if the required file is already available then it won’t create any new file and returns false.
booelan mkdir() Creates the directory, If it is not already available.Otherwise it returns false
boolean isDirectory() Returns true if the java file object represents a directory.
String[] list() Returns the names of files and directories present in the specified directory.
If the java file object represents a file instead of directory this method returns null.
boolean delete() For deleting the file or directory represented by java file object.

Example:1Create a file named with ‘java2learn.txt’ in the current working directory.

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

public class FileDemo {
	public static void main(String[] args) {
		File file = new File("java2learn.txt");
		try {
			file.createNewFile();
		} catch (IOException exception) {
			exception.printStackTrace();
		}

	}
}

Example:2Create a directory ‘java2learn’ in the current working directory and create a file ‘corejava.txt’ in that directory.

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

public class FileDemo {
	public static void main(String[] args) {
		File f = new File("java2learn");
		f.mkdir();
		// File f1 = new File("java2learn","corejava.txt");
		File f1 = new File(f, "corejava.txt");
		try {
			f1.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Example:3Get the list of files and directories in a specified directory.

 
import java.io.File;

public class FileDemo {
	public static void main(String[] args) {
		File file = new File("java2learn");
		String[] filesAndDirs = file.list();
		for (String fileOrDir : filesAndDirs) {
			System.out.println(fileOrDir);
		}
	}
}