📁Java File Handling Programs
Reading and writing files is everyday work in Java. These programs show both the classic java.io readers and writers and the modern java.nio.file helpers, and each one creates and cleans up its own sample files so you can run it anywhere.15 programs · each with full code, output and a step-by-step explanation
Most Asked in Interviews
🔍
Beginner
01
View Program →Append Text to a File
Adds new lines to the end of an existing log file without erasing what's already there.
beginnerFile I/O
02
View Program →Check if a File Exists
Checks whether a file exists, inspects its type and size, then deletes it safely.
beginnerFile I/O
03
View Program →Copy a File
Copies a file with Files.copy and verifies the copy is byte-for-byte identical.
beginnerFile I/O
04
View Program →Read a CSV File
Reads a small sales CSV line by line, skips the header, parses each column and totals units and revenue.
beginnerFile I/O
05
View Program →Read a File Line by Line
Reads a text file one line at a time with BufferedReader.readLine and numbers each line.
beginnerFile I/O
06
View Program →Read a Properties File
Loads application settings from a .properties file and reads them as strings, numbers and booleans with defaults.
beginnerFile I/O
07
View Program →Rename or Move a File
Renames a file within its folder, then moves it into an archive folder, with Files.move.
beginnerFile I/O
08
View Program →Search for a Word in a File
Scans a log file line by line and prints the line number of every line containing ERROR.
beginnerFile I/O
09
View Program →Write a CSV File
Writes rows to a CSV file, quoting fields that contain commas or quotes so the file stays valid.
beginnerFile I/O
10
View Program →Write to a File
Writes several lines to a text file with BufferedWriter and reads them back to confirm.
beginnerFile I/O
Intermediate
11
View Program →Count Lines, Words and Characters in a File
Counts the lines, words and characters of a text file, handling blank lines and extra spaces.
intermediateFile I/O
12
View Program →Delete a Directory Recursively
Deletes a folder and everything inside it by walking the tree and removing the deepest paths first.
intermediateFile I/O
13
View Program →List Files in a Directory
Lists a folder's contents with Files.list, marking which entries are files and which are folders.
intermediateFile I/O
14
View Program →Read and Write Binary Data
Writes an int, a double, a boolean and a string to a binary file with DataOutputStream and reads them back.
intermediateFile I/O
15
View Program →Serialize an Object to a File
Saves an object to a file with ObjectOutputStream and restores it, showing how transient fields are skipped.
intermediateFile I/O