Solved

readLine() and DataInputStream vs BufferedReader

Posted on 1998-08-10
2
2,303 Views
Last Modified: 2012-06-22
I'm reading "Java Network Programming" by Hughes, Schoffner and Winslow (good book).
The book was written 1.0 - not a mention about readLine() deprecation.
Question is do I simply use BufferedReader.readLine() wherever I see
DataInputStream.readLine()?  Is this a no brainer or is there something
to watch out for in network communications that makes this a "maybe"?

Thanks
Thanks
0
Comment
Question by:hank1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
sailwind earned 50 total points
ID: 1232323
In short, yes you can simply use the BufferedReader.readln() to
read in data.

You can use InputStreamReader to read in data, the readln()
will work just fine. In JDK 1.1, we can add additional filters to the
input stream to enhance its capabilities. BufferedReader, for example
will allow you to buffer the incoming bits. This allows for fewer read
operations from the source, and improves the efficiency.

Therefore, to read from the standard input, the following two lines will
be what you want:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in))
String s = in.readLine();

As you can see, inputStreamReader could've done the job of reading the data.
However, piping it through the buffered reader adds more efficiency to it.

For a even more involved example of chaining multiple filters, try this program out:

import java.io.*;

      class Lines {
         static String fileName = "test.in";
         public static void main(String[] args) {
            try {
               FileInputStream in = new FileInputStream(fileName);
               LineNumberInputStream lineIn;
               lineIn = new LineNumberInputStream(in);
               DataInputStream dataIn = new DataInputStream(lineIn);

               while (dataIn.available() > 0) {
                  String s = dataIn.readLine();
                  int lineNum = lineIn.getLineNumber();
                  System.out.println("Line " + lineNum + ": " + s);
               }
            } catch (IOException x) {
               System.out.println(x.getMessage());
            }
         }
      }

Take a look, I'm sure you'll see how it works. If not, just post a message and I'll
explain.
0
 
LVL 1

Author Comment

by:hank1
ID: 1232324
Thanks
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 50
jboss 7.1 start up error 1 64
Eclipse Java import and method not resolved 4 82
IBM TS2900 (3572) Tape Autoloader Java? 12 61
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question