Writing Bytes to Output Streams

The fundamental method of the OutputStream class is write( ) :

public abstract void write(int b) throws IOException

This method writes a single unsigned byte of data whose value should be between 0 and 255. If you pass a number larger than 255 or smaller than 0, it's reduced modulo 256 before being written.

Example 2-1, AsciiChart, is a simple program that writes the printable ASCII characters (32 to 126) on the console. The console interprets the numeric values as ASCII characters, not as numbers. This is a feature of the console, not of the OutputStream class or the specific subclass of which System.out is an instance. The write( ) method merely sends a particular bit pattern to a particular output stream. How that bit pattern is interpreted depends on what's connected to the other end of the stream.

Example 2-1. The AsciiChart program

import java.io.*;
public class AsciiChart {
 public static void main(String[] args) {
 for (int i = 32; i < 127; i++) {
 System.out.write(i);
 // break line after every eight characters.
 if (i % 8 == 7) System.out.write('
');
 else System.out.write('	');
 }
 System.out.write('
');
 }
}

Notice the use of the char literals ' ' and ' '. The compiler converts these to the numbers 9 and 10, respectively. When these numbers are written on the console, the console interprets them as a tab and a linefeed, respectively. The same effect could have been achieved by writing the if clause like this:

if (i % 8 == 7) System.out.write(10);
else System.out.write(9);

Here's the output:

% java AsciiChart
! " # $ % & '
( ) * + , - . /
0 1 2 3 4 5 6 7
8 9 : ; < = > ?
@ A B C D E F G
H I J K L M N O
P Q R S T U V W
X Y Z [  ] ^ _
` a b c d e f g
h i j k l m n o
p q r s t u v w
x y z { | } ~

The write( ) method can throw an IOException, so you'll need to wrap most calls to this method in a try/catch block, or declare that your own method throws IOException. For example:

try {
 for (int i = 32; i <= 127; i++) out.write(i);
}
catch (IOException ex) {
 System.err.println(ex);
}

Observant readers will have noticed that Example 2-1 did not actually catch any IOExceptions. The PrintStream class, of which System.out is an instance, overrides write( ) with a variant that does not throw IOException. This is very unusual, and PrintStream is almost the only class that does this. I'll have more to say about PrintStream, including this very unsafe behavior, in Chapter 7.

Basic I/O

Introducing I/O

Output Streams

Input Streams

Data Sources

File Streams

Network Streams

Filter Streams

Filter Streams

Print Streams

Data Streams

Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O

Buffers

Channels

Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers

Text

Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text

Devices

The Java Communications API

USB

The J2ME Generic Connection Framework

Bluetooth

Character Sets



Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net