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

Working with Input and Output in Java
(Page 1 of 5 )

Day 15: Working with Input and Output

Many of the programs you create with Java need to interact with some kind of data source. Information can be stored on a computer in many ways, including files on a hard drive or CD-ROM, pages on a Web site, and even the computer's memory.

You might expect to need a different technique to handle each different storage device. Fortunately, that isn't the case.

In Java, information can be stored and retrieved using a communications system called streams, which are implemented in the java.io package.

Today you learn how to create input streams to read information and output streams to store information. You'll work with each of the following:

  • Byte streams, which are used to handle bytes, integers, and other simple data types

  • Character streams, which handle text files and other text sources

You can deal with all data the same way when you know how to work with an input stream, whether the information is coming from a disk, the Internet, or even another program. The same holds for using output streams to transmit data.

Note - Additional techniques for input and output programming are offered in the java.nio package. Because this package is most useful in network programming, it will be discussed during Day 17, "Communicating Across the Internet."

Introduction to Streams

In Java all data are written and read using streams. Streams, like the bodies of water that share the same name, carry something from one place to another.

A stream is a path traveled by data in a program. An input stream sends data from a source into a program, and an output stream sends data from a program to a destination.

You will deal with two types of streams today: byte streams and character streams. Byte streams carry integers with values that range from 0 to 255. A diverse assortment of data can be expressed in byte format, including numerical data, executable programs, Internet communications, and bytecode—the class files run by a Java virtual machine.

In fact, every kind of data imaginable can be expressed using either individual bytes or a series of bytes combined with each other.

Character streams are a specialized type of byte stream that handles only textual data. They're distinguished from byte streams because Java's character set supports Unicode, a standard that includes many more characters than could be expressed easily using bytes.

Any kind of data that involves text should use character streams, including text files, Web pages, and other common types of text.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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