Java
  Home arrow Java arrow Page 3 - Using Streams in Java
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Using Streams in Java
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2006-04-20

    Table of Contents:
  • Using Streams in Java
  • Buffered Streams
  • Console Input Streams
  • Data Streams

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Streams in Java - Console Input Streams


    (Page 3 of 4 )

    One of the things many experienced programmers miss when they begin learning Java is the ability to read textual or numeric input from the console while running an application. There is no input method comparable to the output methods System.out.print() and System.out.println().

    Now that you can work with buffered input streams, you can put them to use receiving console input.

    The System class, part of the java.lang package, has a class variable called in that is an InputStream object. This object receives input from the keyboard through the stream.

    You can work with this stream as you would any other input stream. The following statement creates a new buffered input stream associated with the System.in input stream:

    BufferedInputStream command =
    new BufferedInputStream(System.in);

    The next project, the ConsoleInput class, contains a class method you can use to receive console input in any of your Java applications. Enter the text of Listing 15.4 in your editor and save the file as ConsoleInput.java.

    Listing 15.4 The Full Text of ConsoleInput.java

     1: import java.io.*;
    2: 
    3: public class ConsoleInput {
    4:   public static String readLine() {
    5:     StringBuffer response = new StringBuffer();
    6:     try {
    7:       BufferedInputStream buff = new
    8:         BufferedInputStream(System.in);
    9:       int in = 0;
    10:       char inChar;
    11:       do {
    12:         in = buff.read();
    13:         inChar = (char) in;
    14:         if (in != -1) {
    15:           response.append(inChar);
    16:         }       
    17:       } while ((in != -1) & (inChar != '\n'));
    18:       buff.close();
    19:       return response.toString();
    20:     } catch (IOException e) {
    21:       System.out.println("Exception: " +
    e.getMessage()); 22: return null; 23: } 24: } 25: 26: public static void main(String[] arguments) { 27: System.out.print("\nWhat is your name? "); 28: String input = ConsoleInput.readLine(); 29: System.out.println("\nHello, " + input); 30: } 31: }

    The ConsoleInput class includes a main() method that demonstrates how it can be used. When you compile and run it as an application, the output should resemble the following:

    What is your name? Amerigo Vespucci
    Hello, Amerigo Vespucci

    More Java Articles
    More By Sams Publishing


       · This article is an excerpt from the book "Sams Teach Yourself Java 2 in 21 days,"...
     

    Buy this book now. This article is excerpted from chapter 15 of Sams Teach Yourself Java 2 in 21 Days, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek