~/ learn/ java-from-zero/ cards/ Reading & writing text with try-with-resources
1 of 4

Type a try-with-resources file-reading loop

Type a try-with-resources file-reading loop

Answer

try (BufferedReader br = new BufferedReader(new FileReader("f.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } }

readLine returns null (not an exception) at EOF. The loop condition assigns and tests in one step. br.close() is called regardless of outcome.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/4be45e35-c110-4e2f-9205-5c7609b77ef3/flashcard utf-8 LF