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

Java File Handling Introduction

File handling is how your program creates, checks, and deletes files and folders, like managing paper in a filing cabinet. Java has a File tool for this.
Syntax
java
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

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

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

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

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());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting that new File(...) does not create a file on disk.
  2. Expecting new File("a.txt") to fail when the file is missing, when it only creates an object and exists() must be checked.
  3. Using the wrong path separators, when File.separator or Paths.get() is safer.

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.