Skip to content

About

Example code for the HappyCoders article “Java BufferedReader” – readLine(), lines(), character encoding, console input, BufferedReader vs. Scanner, and JMH benchmarks

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Java BufferedReader

Build Maintainability Rating Reliability Rating Security Rating

Article

The code refers to the following articles (not yet published):

Contents

Most examples read the small log file files/server.log. Every class with a main() method in eu.happycoders.bufferedreader is one chapter of the article and prints the outputs shown there. Run one from the repository root with java -cp target/classes eu.happycoders.bufferedreader.<ClassName> after mvn compile. Files the examples create go into target/.

  • Intro – reads the log file line by line
  • Ch1Creating – Files.newBufferedReader(), FileReader, InputStreamReader around a GZIPInputStream, an own buffer size, and a Latin-1 file read as UTF-8
  • Ch2ReadLine – line terminators, empty lines, and the end of the input
  • Ch3Lines – lines() with filter() and groupingBy(), an UncheckedIOException while streaming, and Files.lines()
  • Ch4ReadAll – readAllLines() and readAllAsString() (Java 25), also after a readLine()
  • Ch5Characters – read() and read(char[], int, int)
  • Ch6MarkReset – an optional header line with mark() and reset(), and skip()
  • Ch6Ready – ready() returns false, yet the next readLine() returns a line
  • Ch7Console – reading lines from System.in with the charset from stdin.encoding (Java 25)
  • Ch7Readln – IO.readln() (Java 25)
  • Ch8Scanner – tokens with Scanner and with split(), and how both classes report an IOException
  • Ch9Closing – closing the BufferedReader closes the reader inside it
  • Ch10Mistakes – calling readLine() twice per loop iteration

Benchmarks

The package eu.happycoders.bufferedreader.benchmark contains the JMH benchmarks of the article's performance section:

  • ReadBenchmark – reading a 100,000,000-byte UTF-8 text file with InputStreamReader, FileReader, and BufferedReader (read(), read(char[]), readLine(), lines(), readAllLines()), and with Scanner.nextLine()
  • BufferSizeBenchmark – readLine() with buffer sizes from 256 to 1,048,576 characters
  • ReadCallCounter – not JMH: counts how often the InputStreamReader reads from the file when a BufferedReader reads it line by line, for buffer sizes from 256 to 1,048,576 characters; the count is the same for every size
  • DecoderWarmupCheck – not JMH: decodes the test file with an InputStreamReader in blocks of one size after warming the JVM up with another, one JVM per call; it shows that the speed depends on the warm-up block size, not on the measured one (the command is in the class comment)

The first run creates the test file target/benchmark-input.txt from a fixed seed, so every machine reads the same file. Build and run:

mvn package
java -jar target/benchmarks.jar

The complete run takes about 15 minutes. Close other applications while it runs. The results of the article's run are in results/apple-m5-pro/.


Additional Resources


Java Versions PDF Cheat Sheet

Stay up-to-date with the latest Java features with this free PDF Cheat Sheet!

Java Versions PDF Cheat Sheet Mockup

  • Avoid lengthy research with this concise overview of all Java versions from Java 10 to Java 27.
  • Discover the innovative features of each new Java version, summarized on a single page.
  • Impress your team with your up-to-date knowledge of the latest Java version.

👉 Download the free Java Versions PDF

(Hier geht's zur deutschen Version → Java-Versionen PDF)


The Big O Cheat Sheet

With this free 1-page PDF cheat sheet, you'll always have the 7 most important complexity classes at a glance.

Big O PDF Cheat Sheet Mockup

  • Always choose the most efficient data structures and thus increase the performance of your applications.
  • Be prepared for technical interviews and confidently present your algorithm knowledge.
  • Become a sought-after problem solver and be known for systematically tackling complex problems.

👉 Download the free Big O Cheat Sheet

(Hier geht's zur deutschen Version → O-Notation Cheat Sheet)


HappyCoders Newsletter

👉 Want to stay on top of modern Java? Sign up for the HappyCoders newsletter – Modern Java: new versions & features, performance, and JVM insights – once a month.

(Hier geht's zur deutschen Version → HappyCoders-Newsletter deutsch)


🇩🇪 An alle Java-Entwickler:innen, die durch fundierte Kenntnisse über Datenstrukturen besseren Code schreiben wollen

Trage dich jetzt unverbindlich auf die Warteliste von „Mastering Data Structures in Java“ ein, und erhalte das beste Angebot!

Mastering Data Structures Mockup

👉 Zur Warteliste

About

Example code for the HappyCoders article “Java BufferedReader” – readLine(), lines(), character encoding, console input, BufferedReader vs. Scanner, and JMH benchmarks

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages