Java File Handling Introduction
In this page:
File file = new File("file_name");
file.exists();
file.getName();
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 so it can be used by its short name
import java.io.File;
// Import java.io.IOException so it can be used by its short name
import java.io.IOException;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) throws IOException {
// Create a new File object and store it in file
File file = new File("notes.txt");
file.createNewFile();
// Print a line to the console
System.out.println(file.length());
// Print a line to the console
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 so it can be used by its short name
import java.io.File;
// Import java.io.FileWriter so it can be used by its short name
import java.io.FileWriter;
import java.io.IOException;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new File object and store it in file
File file = new File("notes.txt");
// Try code that might throw an exception
try {
// Check whether !file.exists()
if (!file.exists()) {
file.createNewFile();
}
// Create a new FileWriter object and store it in writer
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 so it can be used by its short name
import java.io.File;
// Import java.io.IOException so it can be used by its short name
import java.io.IOException;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) throws IOException {
// Create a new File object and store it in file
File file = new File("notes.txt");
file.createNewFile();
// Declare deleted and set it to file.delete()
boolean deleted = file.delete();
// Print a line to the console
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
- Forgetting that
new File(...)does not create a file on disk. - Expecting
new File("a.txt")to fail when the file is missing, when it only creates an object andexists()must be checked. - Using the wrong path separators, when
File.separatororPaths.get()is safer.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: