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
Solved

readLine() and DataInputStream vs BufferedReader

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

790 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