Solved

readLine() and DataInputStream vs BufferedReader

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java JRE greater than 1.6 5 62
Why my table column Id is not passed to java object? 4 39
hibernate example using maven 12 42
eclipse buid path vs tomcat lib path 10 23
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

773 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