Java File Handling Introduction
In this page:
Creating a File
Java's File class represents a path on the filesystem — creating a File object doesn't create an actual file until you call a method like createNewFile(), which returns false if a file at that path already exists.
Example: Creating a File
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File file = new File("notes.txt"); // no file yet, just a path reference
boolean created = file.createNewFile();
System.out.println(created);
}
}
Login to try C/C++/Java/PHP code in the editor
File Properties and Metadata
A File object exposes metadata like length() (size in bytes), lastModified(), and canRead()/canWrite(), letting you inspect a file's properties without opening it.
Example: File Properties and Metadata
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File file = new File("notes.txt");
file.createNewFile();
System.out.println(file.length());
System.out.println(file.canRead());
}
}
Login to try C/C++/Java/PHP code in the editor
Writing check parameters
Before writing to a file, it's worth checking exists() and handling IOException, since file operations can fail for reasons outside your program's control — permissions, missing directories, or a full disk.
Example: Writing check parameters
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
File file = new File("notes.txt");
try {
if (!file.exists()) {
file.createNewFile();
}
FileWriter writer = new FileWriter(file);
writer.write("Hello");
writer.close();
} catch (IOException e) {
System.out.println("Write failed: " + e.getMessage());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Deleting a File
delete() permanently removes a file (or an empty directory) and returns a boolean indicating success — it fails silently rather than throwing, so always check the return value if the deletion matters.
Example: Deleting a File
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File file = new File("notes.txt");
file.createNewFile();
boolean deleted = file.delete();
System.out.println(deleted);
}
}
Login to try C/C++/Java/PHP code in the editor
Working with Directories
mkdir() creates a single directory while mkdirs() creates any missing parent directories along the path too — use mkdirs() unless you're certain the parent structure already exists.
Example: Working with Directories
import java.io.File;
public class Main {
public static void main(String[] args) {
File single = new File("folder1");
single.mkdir();
File nested = new File("folder2/subfolder");
nested.mkdirs(); // creates missing parent too
System.out.println(nested.exists());
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: