← Back to Core Java Course | Chapter 12: File I/O | Lesson 2 of 9

Java File Handling Introduction

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

java
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);
	}
}

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

java
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());
	}
}

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

java
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());
		}
	}
}

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

java
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);
	}
}

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

java
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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.