File Class Methods (Java java.io.File)
The java.io.File class is used to work with files and directories (folders).
It is used for:
- Checking if file/directory exists
- Creating files and folders
- Deleting files/folders
- Getting file metadata (size, name, path)
- Listing directory contents
Below are the most important File class methods grouped for easy learning.

1. File Information Methods
exists()
Checks whether the file or directory physically exists on the disk. Returns true if present, otherwise false.
file.exists();
getName()
Returns only the name of the file or directory (without path).
file.getName();
getPath()
Returns the path exactly as given in the File constructor (may be relative or absolute).
file.getPath();
getAbsolutePath()
Returns the complete absolute path of the file/directory from the root of the file system.
file.getAbsolutePath();
length()
- Returns the size of the file in bytes.
- Does not work for folders (returns 0 for directories).
file.length();
lastModified()
Returns the last modified timestamp of the file in milliseconds (epoch time).
2. Create Methods
createNewFile()
- Creates a new empty file on disk.
- Returns true if file is created, false if file already exists.
file.createNewFile();
mkdir()
- Creates a single directory.
- Fails if parent directory does not exist.
file.mkdir();
mkdirs()
- Creates directory including all necessary parent directories.
- Useful when creating nested folder structures.
file.mkdirs();
3. Delete Methods
delete()
- Deletes the file or an empty directory.
- Returns true if deletion is successful.
file.delete();
deleteOnExit()
Schedules the file for deletion when the JVM terminates. Useful for temporary files.
file.deleteOnExit();
4. Check Type & Properties
isFile()
Returns true if the File object represents a regular file (not a directory).
file.isFile();
isDirectory()
Returns true if the File object represents a directory/folder.
file.isDirectory();
canRead()
Checks if the file/directory has read permission.
canWrite()
Checks if the file/directory has write permission.
canExecute()
Checks if the file/directory can be executed (mainly for programs/scripts).
isHidden()
Returns true if the file is hidden by the operating system.
5. Directory Methods
list()
Returns an array of String names of all files and folders inside the directory.
String[] files = dir.list();
listFiles()
- Returns an array of File objects representing all contents in the folder.
- Useful for file processing.
File[] files = dir.listFiles();
listFiles(FileFilter filter)
Returns only the files that satisfy a custom filter condition (e.g., only .txt files).
6. Rename / Move File
renameTo(File newFile)
- Renames the file or moves it to a different location/path.
- Returns true if successful; may fail across different drives.
file.renameTo(new File("newname.txt"));
Example: Using Common File Methods
import java.io.File;
public class FileMethodsDemo {
public static void main(String[] args) {
File file = new File("test.txt");
System.out.println("Exists: " + file.exists());
System.out.println("Name: " + file.getName());
System.out.println("Absolute Path: " + file.getAbsolutePath());
System.out.println("Is File: " + file.isFile());
System.out.println("Is Directory: " + file.isDirectory());
System.out.println("Size: " + file.length());
}
}
Most Important File Class Methods (Short List)
If your chapter needs only the key ones:
exists()createNewFile()mkdir()/mkdirs()delete()isFile()/isDirectory()list()/listFiles()getName()getAbsolutePath()length()
