public voidflush()throws IOException
Flushes the stream. If the stream has buffered any bytes from the various
write methods, flush writes them immediately to their destination. Then,
if that destination is another stream, it is also flushed. One flush invocation
will flush all the buffers in a chain of streams. If the stream is not buffered,
flush may do nothingthe default implementation. This method is defined in
the Flushable interface.
public voidclose()tHRows IOException
Closes the output stream. This method should be invoked to release any
resources (such as file descriptors) associated with the stream. Once a stream
has been closed, further operations on the stream will throw an
IOException. Closing a previously closed stream has no effect.The
default implementation of close does nothing.
The implementation of OutputStream requires only that a subclass provide the single-byte variant of
write because the other write methods are defined in terms of this one. Most streams, however, can
improve performance by overriding other methods as well. The default implementations of flush and
close will usually need to be overridden as appropriate for a particular streamin particular, buffered streams
may need to flush when closed.
Here is a program that copies its input to its output, translating one particular byte value to a different one
along the way. The TRanslateByte program takes two parameters: a from byte and a to byte. Bytes that
match the value in the string from are translated into the value in the string to.
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!