O'Reilly logo

Core Java® Volume II—Advanced Features, Ninth Edition by Gary Cornell, Cay S. Horstmann

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

1.2.2. How to Read Text Input

You already know that:

• To write data in binary format, you use a DataOutputStream.

• To write in text format, you use a PrintWriter.

Therefore, you might expect that there is an analog to the DataInputStream that lets you read data in text format. The closest analog is the Scanner class that we used extensively in Volume I. However, before Java SE 5.0, the only game in town for processing text input was the BufferedReader class with the readLine method that lets you read a line of text. You need to combine a buffered reader with an input source.

BufferedReader in = new BufferedReader(    new InputStreamReader(new FileInputStream("employee.txt"), "UTF-8));

The readLine method returns ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required