LearnFrenzy
Sign in Take a free test

File Handling, IO and NIO · Exercise 4 of 4

File Handling, IO and NIO File Handling Programs

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Java programs that read, write and organise files with java.io and java.nio.file. Each program works inside a temporary folder. Write each one yourself first, then open the solution – every solution was compiled and run on Java 21, and the output shown is the real output.

Question 1 Easy

Write a text to a temporary file, then read it back with a BufferedReader and print the number of lines, words and characters (like the Unix wc command).

Question page & discussion
Question 2 Medium

Write sales.csv (region,product,amount), read it with Files.lines, total the amount per region, and write a sorted report.txt with PrintWriter; print the report.

Question page & discussion
Question 3 Medium

Create a 10,000-byte binary file, copy it using an 4 KB byte buffer with InputStream and OutputStream, count the read() calls, and verify the copy matches the original.

Question page & discussion
Question 4 Medium

Write an app.conf file with comments, blank lines and key = value pairs, parse it into a LinkedHashMap (trimming spaces, ignoring bad lines), and print typed settings with defaults.

Question page & discussion
Question 5 Medium

Read app.log with a BufferedReader, write only ERROR lines (with their line numbers) to errors.log using a BufferedWriter, and print a summary.

Question page & discussion
Question 6 Medium

Create a small project tree, then use Files.walk to count files and total bytes per extension (sorted), and find the largest file.

Question page & discussion
Question 7 Hard

Write a list of Student records to a binary file with DataOutputStream (a count, then name, roll and marks for each), read them back with DataInputStream, and print the class topper.

Question page & discussion
Question 8 Easy

Create three chapter files, merge them in name order into book.txt with a "== file ==" header before each, and print the merged file and its line count.

Question page & discussion
Question 9 Hard

Update a counter file safely: write the new content to a temporary file in the same folder, then atomically move it over the original, so a crash can never leave a half-written file. Run five updates.

Question page & discussion
Question 10 Easy

Write a sentence to a file, read it character by character with a buffered Reader, count each letter (ignoring case), and print a histogram of the five most common letters.

Question page & discussion
All exercises doneBack to File Handling, IO and NIORevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links