Java File Handling Practice
Reading and writing files with File, FileWriter, Scanner and BufferedReader.
1
Write a File
Easy
Read a line of text and a file name. Write the text to that file using FileWriter, then print "Written".
Input Format
Two lines: file name, then content.
Output Format
Written
Sample Test Cases
|
Sample Input 1
note.txt Hello Java |
Sample Output 1
Written |
Constraints
File name length
Explanation
new FileWriter(name) then write().
2
Read a File (Existing)
Easy
Assume file "data.txt" already exists with one line. Read the file with Scanner and print its single line. If the file is missing, print "File not found".
Input Format
No input (reads data.txt from disk).
Output Format
The file content or "File not found".
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Hello from file |
Constraints
data.txt may or may not exist.
Explanation
Scanner over the File object.
1
Append to File
Medium
Read two lines (existing content and a file name). Append a new line with the text "Appended: " to that file, then print "Done".
Input Format
Two lines: file name, then content.
Output Format
Done
Sample Test Cases
|
Sample Input 1
log.txt hello |
Sample Output 1
Done |
Constraints
File name length
Explanation
new FileWriter(name, true) appends.
2
Read Numbers from File
Medium
Assume "nums.txt" contains N on the first line and N integers on the second. Read them and print their sum.
Input Format
No input (reads nums.txt).
Output Format
The sum.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
15 |
Constraints
N
Explanation
Use Scanner over the file.
3
Copy File
Medium
Read source and destination file names. Copy the content of the source to the destination and print "Copied". If the source does not exist, print "Source not found".
Input Format
Two lines: source, then destination.
Output Format
Copied or Source not found.
Sample Test Cases
|
Sample Input 1
a.txt b.txt |
Sample Output 1
Copied |
Constraints
File names
Explanation
Read all lines, write to destination.
1
Count Lines in File
Hard
Read a file name. Count and print the number of lines in it using BufferedReader. If the file is missing, print "File not found".
Input Format
A single line: file name.
Output Format
The line count, or "File not found".
Sample Test Cases
|
Sample Input 1
data.txt |
Sample Output 1
3 |
Constraints
Existing file
Explanation
BufferedReader.readLine() until null.
2
Word Count in File
Hard
Read a file name. Count and print the number of words in the file (split each line by spaces). If missing, print "File not found".
Input Format
A single line: file name.
Output Format
The word count.
Sample Test Cases
|
Sample Input 1
data.txt |
Sample Output 1
7 |
Constraints
Existing file.
Explanation
Sum split(...).length over lines.
3
Delete a File
Hard
Read a file name. Delete it with File.delete() and print "Deleted" on success or "Not found" otherwise.
Input Format
A single line: file name.
Output Format
Deleted or Not found.
Sample Test Cases
|
Sample Input 1
temp.txt |
Sample Output 1
Deleted |
Constraints
File name
Explanation
new File(name).delete().
Competitive MCQs — Java File Handling
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 31
Q1
Which class reads character data from a file?
Correct!
Wrong — correct answer is .
FileReader wrapped in BufferedReader reads text files.
Q2
Which package contains file classes?
Correct!
Wrong — correct answer is .
File I/O classes live in java.io (and java.nio).
Q3
Which exception must be handled when reading a file?
Correct!
Wrong — correct answer is .
File operations throw checked IOException.
Q4
What is the output of this code?
java
1
2
2
File f = new File("data.txt");
System.out.println(f.exists());Correct!
Wrong — correct answer is .
exists() reports whether the file is present.
Q5
What is the output of this code?
File f = new File("data.txt");
System.out.println(f.exists());
File f = new File("data.txt");
System.out.println(f.exists());
Correct!
Wrong — correct answer is .
exists() reports whether the file is present.
Q6
Which class writes text to a file conveniently?
Correct!
Wrong — correct answer is .
FileWriter writes characters to a file.
Q7
Which statement safely closes a file automatically?
Correct!
Wrong — correct answer is .
try (var r = ...) {} auto-closes resources since Java 7.
Q8
Which method reads one whole line from a BufferedReader?
Correct!
Wrong — correct answer is .
BufferedReader.readLine() returns a line or null at EOF.
Q9
What does new File("x.txt").createNewFile() do if x.txt exists?
Correct!
Wrong — correct answer is .
createNewFile() creates only if absent and returns a boolean.
Q10
Which method deletes a file?
Correct!
Wrong — correct answer is .
File.delete() removes the file and returns boolean.
Q11
What is the output of this code?
java
1
2
2
File f = new File("nonexistent.txt");
System.out.println(f.exists());Correct!
Wrong — correct answer is .
A missing file means exists() returns false.
Q12
What is the output of this code?
File f = new File("nonexistent.txt");
System.out.println(f.exists());
File f = new File("nonexistent.txt");
System.out.println(f.exists());
Correct!
Wrong — correct answer is .
A missing file means exists() returns false.
Q13
Which class writes text to a file?
Correct!
Wrong — correct answer is .
FileWriter writes character data to files.
Q14
What is try-with-resources?
Correct!
Wrong — correct answer is .
try (var r = ...) {} closes resources automatically.
Q15
Which package is for NIO file APIs?
Correct!
Wrong — correct answer is .
java.nio.file provides modern Files/Path APIs.
Q16
What does Files.readAllLines(Path) return?
Correct!
Wrong — correct answer is .
readAllLines reads the whole file into a list of lines.
Q17
What is the output of this code?
File f = new File("data.txt");
f.createNewFile();
System.out.println(f.exists());
File f = new File("data.txt");
f.createNewFile();
System.out.println(f.exists());
Correct!
Wrong — correct answer is .
createNewFile() makes the file, so exists() becomes true.
Q18
Which exception must a file-read method declare?
Correct!
Wrong — correct answer is .
File operations throw the checked IOException.
Q19
What does a File object represent?
Correct!
Wrong — correct answer is .
File is a path abstraction; the actual file may not exist.
Q20
What is the output of this code?
File d = new File("folder");
System.out.println(d.isDirectory());
File d = new File("folder");
System.out.println(d.isDirectory());
Correct!
Wrong — correct answer is .
isDirectory() reports whether the path is an existing directory.
Q21
Which method lists files inside a directory?
Correct!
Wrong — correct answer is .
listFiles() returns an array of File entries inside the directory.
Q22
What does delete() return on success?
Correct!
Wrong — correct answer is .
File.delete() returns a boolean success indicator.
Q23
Which class buffers character input for efficiency?
Correct!
Wrong — correct answer is .
BufferedReader wraps readers to reduce disk reads.
Q24
What is the output of this code?
File f = new File(".");
System.out.println(f.exists());
File f = new File(".");
System.out.println(f.exists());
Correct!
Wrong — correct answer is .
The current directory path exists, so exists() returns true.
Q25
Which is the correct way to copy a file with NIO?
Correct!
Wrong — correct answer is .
java.nio.file.Files.copy handles file copying.
Q26
What does Files.write(Path, bytes) do?
Correct!
Wrong — correct answer is .
Files.write outputs the given bytes to the path.
Q27
Which mode does FileWriter use by default?
Correct!
Wrong — correct answer is .
FileWriter overwrites unless constructed with append=true.
Q28
What is the output of this code?
BufferedWriter w = new BufferedWriter(new FileWriter("a.txt"));
w.write("hi");
w.close();
BufferedWriter w = new BufferedWriter(new FileWriter("a.txt"));
w.write("hi");
w.close();
Correct!
Wrong — correct answer is .
The writer writes "hi" and closes the file.
Q29
Which interface represents a byte input stream?
Correct!
Wrong — correct answer is .
InputStream reads raw bytes; Reader reads characters.
Q30
What does a Path represent in NIO?
Correct!
Wrong — correct answer is .
Path abstracts a filesystem location for NIO operations.
Q31
Which method moves a file?
Correct!
Wrong — correct answer is .
Files.move relocates or renames a file.