Working with Input and Output in Java
(Page 1 of 5 )
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).
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 StreamsIn 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.
Next: Using a Stream >>
More Java Articles
More By Sams Publishing
|
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.
|
|