File Class
The File class in Java (from the java.io package) represents a file or directory path in the file system. It is used to create, delete, inspect, and manage files/directories, but does not read or write file content. This is a high-frequency interview topic, often confused with file I/O streams.
What Is the File Class?
- Represents file or directory metadata
- Works with paths, not content
- Platform-independent (uses OS-specific separators internally)
- Part of java.io package
File file = new File("data.txt");
What File Can and Cannot Do
✔ Can Do
- Check existence
- Create/delete files & directories
- Get file properties (name, size, path, permissions)
- List directory contents
❌ Cannot Do
- Read file data
- Write file data
(Use streams/readers/writers for content I/O)
Creating a File Object
1️⃣ Using Relative Path
File f = new File("data.txt");
✔ Path relative to current working directory
2️⃣ Using Absolute Path
File f = new File("C:\\files\\data.txt");
3️⃣ Using Parent + Child
File f = new File("C:\\files", "data.txt");
✔ Cleaner and safer path handling
Commonly Used Methods (Must-Know)
Existence & Type Checks
f.exists(); // true/false
f.isFile(); // file?
f.isDirectory(); // directory?
File Information
f.getName();
f.getPath();
f.getAbsolutePath();
f.length(); // size in bytes
f.lastModified();
Permissions
f.canRead();
f.canWrite();
f.canExecute();
Creating Files and Directories
Create a File
File f = new File("test.txt");
f.createNewFile(); // throws IOException
✔ Creates empty file if not exists
❌ Returns false if already exists
Create Directory
File dir = new File("logs");
dir.mkdir(); // single directory
Create Directory Tree
File dir = new File("a/b/c");
dir.mkdirs(); // creates parent directories too
Deleting Files and Directories
f.delete();
⚠ Directory must be empty to delete successfully
Listing Directory Contents
List Names
File dir = new File("C:\\files");
String[] names = dir.list();
List Files as File Objects (Preferred)
File[] files = dir.listFiles();
✔ Allows further inspection
Renaming / Moving Files
File oldFile = new File("old.txt");
File newFile = new File("new.txt");
oldFile.renameTo(newFile);
✔ Also works as move if paths differ
⚠ Behavior may vary across OS
File Separator (Platform-Independent)
File.separator
✔ Avoid hardcoding / or \\
File vs Streams (Interview Trap)
| Aspect | File | Streams / Readers |
|---|---|---|
| Purpose | Metadata & path | Read/write content |
| Package | java.io | java.io, java.nio |
| Reads data | ❌ No | ✔ Yes |
| Writes data | ❌ No | ✔ Yes |
File vs Path (Modern Java)
| Aspect | File | Path (java.nio.file) |
|---|---|---|
| Introduced | Java 1.0 | Java 7 |
| API | Limited | Rich & modern |
| Exception handling | Weak | Strong |
| Recommendation | Legacy | Preferred |
✔ Use Path + Files for new code
✔ Still must know File for interviews
Common Beginner Mistakes
- Assuming File reads/writes content
- Forgetting createNewFile() throws exception
- Deleting non-empty directories
- Hardcoding path separators
- Ignoring return values of mkdir() / delete()
Interview-Ready Answers
Short Answer
The File class represents a file or directory path in Java.
Detailed Answer
In Java, the File class from the java.io package is used to represent file and directory paths. It provides methods to create, delete, and inspect files and directories, but it does not perform actual file reading or writing. For content I/O, streams or NIO APIs are used.
Key Takeaway
File is about paths and metadata, not data.
Use it to manage files and directories, and combine it with streams or NIO for actual I/O operations.