Java
  Home arrow Java arrow Page 2 - Working with Input and Output 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

Working with Input and Output in Java
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2006-04-13

    Table of Contents:
  • Working with Input and Output in Java
  • Using a Stream
  • Handling Exceptions
  • File Input Streams
  • File Output 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


    Working with Input and Output in Java - Using a Stream


    (Page 2 of 5 )

    The procedure for using either a byte stream or a character stream in Java is largely the same. Before you start working with the specifics of the java.io classes, it's useful to walk through the process of creating and using streams.

    For an input stream, the first step is to create an object associated with the data source. For example, if the source is a file on your hard drive, a FileInputStream object could be associated with this file.

    After you have a stream object, you can read information from that stream by using one of the object's methods. FileInputStream includes a read() method that returns a byte read from the file.

    When you're finished reading information from the stream, you call the close() method to indicate that you're finished using the stream.

    For an output stream, you begin by creating an object associated with the data's destination. One such object can be created from the BufferedWriter class, which represents an efficient way to create text files.

    The write() method is the simplest way to send information to the output stream's destination. For instance, a BufferedWriter write() method can send individual characters to an output stream.

    As with input streams, the close() method is called on an output stream when you have no more information to send.

    Filtering a Stream

    The simplest way to use a stream is to create it and then call its methods to send or receive data, depending on whether it's an output stream or an input stream.

    Many of the classes you will work with today achieve more sophisticated results when a filter is associated with a stream before reading or writing any data.

    A filter is a type of stream that modifies the way an existing stream is handled. Think of a dam on a mountain stream. The dam regulates the flow of water from the points upstream to the points downstream. The dam is a type of filter—remove it, and the water would flow in a much less controlled fashion.

    The procedure for using a filter on a stream is as follows:

    1. Create a stream associated with a data source or a data destination.

    2. Associate a filter with that stream.

    3. Read or write data from the filter rather than the original stream.

    The methods you call on a filter are the same as the methods you would call on a stream. There are read() and write() methods, just as there would be on an unfiltered stream.

    You can even associate a filter with another filter, so the following path for information is possible: an input stream associated with a text file, which is filtered through a Spanish-to-English translation filter, which is then filtered through a no-profanity filter, and is finally sent to its destination—a human being who wants to read it.

    If this is confusing in the abstract, you will have opportunities to see the process in practice in the following sections.

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek