Home arrow Java arrow Page 2 - Working with Input and Output in Java
JAVA

Working with Input and Output in Java


This article, the first of three parts, will teach you how the programs you create in Java can interact with different storage devices using a communications system called streams. It is excerpted from chapter 15 of the book Sams Teach Yourself Java 2 in 21 days, written by Roger Cadenhead and Laura Lemay (Sams, ISBN: 0672326280).

Author Info:
By: Sams Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 12
April 13, 2006
TABLE OF CONTENTS:
  1. · Working with Input and Output in Java
  2. · Using a Stream
  3. · Handling Exceptions
  4. · File Input Streams
  5. · File Output Streams

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials