?
Solved

readLine() and DataInputStream vs BufferedReader

Posted on 1998-08-10
2
Medium Priority
?
2,333 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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

650 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