Java Reference
In-Depth Information
import java.io.*;
class TranslateByte {
public static void main(String[] args)
throws IOException
{
byte from = (byte) args[0].charAt(0);
byte to = (byte) args[1].charAt(0);
int b;
while ((b = System.in.read()) != -1)
System.out.write(b == from ? to : b);
}
}
For example, if we invoked the program as
java TranslateByte b B
and entered the text abracadabra! , we would get the output
aBracadaBra!
Manipulating data from a stream after it has been read, or before it is
written, is often achieved by writing Filter streams, rather than hard-
coding the manipulation in a program. You'll learn about filters in Sec-
tion 20.5.2 on page 516 .
Exercise 20.1 : Rewrite the TRanslateByte program as a method that
translates the contents of an InputStream onto an OutputStream , in which
the mapping and the streams are parameters. For each type of In-
putStream and OutputStream you read about in this chapter, write a new
main method that uses the translation method to operate on a stream of
 
Search WWH ::




Custom Search