Solved

readLine() and DataInputStream vs BufferedReader

Posted on 1998-08-10
2
2,279 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
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now