File I/O
Computer Science · Java
Question 1 of 10
0What number does this Java code print? import java.io.*; public class Main { public static void main(String[] args) throws IOException { String text = "jolly\necho\nbravo\nhotel"; BufferedReader reader = new BufferedReader(new StringReader(text)); String line; int total = 0; while ((line = reader.readLine()) != null) { total++; } System.out.println(total); } }
Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.
How to type your answer
- Fractions: 3/4. Mixed numbers: 1 1/2.
- Decimals with a point or a comma: 0.75 or 0,75.
- Negative numbers start with a minus: -3/4.
- Percentages need the % sign: 75%.
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
File I/O explained
Java reads and writes files with classes from java.nio.file and java.io. The modern approach uses Path objects with the static methods of the Files class. Files.readString and Files.writeString, available from Java 11, handle a whole file in one call.
Files.readString(path) returns the whole file as one String, and Files.writeString(path, text) creates the file or replaces its contents. Both methods can throw IOException, which is a checked exception, so the calling method must catch it or declare throws IOException. Passing StandardOpenOption.APPEND to writeString adds text to the end of the file instead. Files.exists tests whether a file is present before you read it. For streams you open yourself, try-with-resources closes them when the block ends, even if an exception occurs. Forgetting to close a writer can leave buffered text unwritten.
import java.nio.file.*;
import java.io.IOException;
public static void main(String[] args) throws IOException {
Path p = Path.of("scores.txt");
Files.writeString(p, "Ana 7\nBen 9\n");
String text = Files.readString(p);
System.out.println(text.lines().count());
}
Worked example
Trace the program above.
- writeString creates scores.txt containing the two lines "Ana 7" and "Ben 9".
- readString returns the whole content as one String, including both newline characters.
- lines() splits the text into a stream of lines, which contains 2 elements.
- count() returns 2, so the output is 2.
What you will practise
New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.
Example questions
Easy
What number does this Java code print?
import java.io.*;
public class Main { public static void main(String[] args) throws IOException { String text = "echo\nfjord\nivory\necho\ndelta"; BufferedReader reader = new BufferedReader(new StringReader(text)); String line; int total = 0; while ((line = reader.readLine()) != null) { total++; } System.out.println(total); } }
Medium
What number does this Java code print?
import java.io.*;
public class Main { public static void main(String[] args) throws IOException { String text = "timber\ntim\nroc\nbridge"; BufferedReader reader = new BufferedReader(new StringReader(text)); String line; int total = 0; while ((line = reader.readLine()) != null) { total += line.length(); } System.out.println(total); } }
Hard
What number does this Java code print?
import java.io.*;
public class Main { public static void main(String[] args) throws IOException { String text = " delta \nhotel\ndelta\n \n alpha \n kilo "; BufferedReader reader = new BufferedReader(new StringReader(text)); String line; int total = 0; while ((line = reader.readLine()) != null) { String t = line.trim(); if (t.isEmpty() || t.startsWith("#")) { continue; } total += t.length(); } System.out.println(total); } }