O u t p u t S t ream j a v a. i o. O u t p u t S t r e a m w r i t e () f l u s h () c l o s e () public abstract void write(int b) throws IOException public void write(byte[] data) throws IOException public void write(byte[] data, int offset, int length) throws IOException public void flush() throws IOException public void close() throws IOException 31
3 2 O u t p u t S t r e a m write(int b) F i l e O u t p u t S t r e a m O u t p u t S t r e a m O u t p u t S t r e a m O u t p u t S t r e a m j a v a. n e t. U R L C o n n e c t i o n g e t O u t p u t S t r e a m () public OutputStream getoutputstream() throws IOException U R L C o n n e c t i o n s u n. n e t. T e l n e t O u t p u t S t r e a m s u n. n e t. w w w. h t t p. K e e p A l i v e S t r e a m s u n. n e t. s m t p. S m t p P r i n t S t r e a m O u t p u t S t r e a m s u n O u t p u t S t r e a m j a v a. i o. D a t a O u t p u t S t r e a m c l o s e () c l o s e () Writing Bytes to Output Stre a m O u t p u t S t r e a m w r i t e ( ) public abstract void write(int b) throws IOException
3 3 A s c i i C h a r t O u t p u t S t r e a m S y s t e m. o u t w r i t e () import java.io.*; public class AsciiChart { public static void main(string[] args) { for (int i = 32; i < 127; i++) { System.out.write(i); // 8 if (i % 8 == 7) System.out.write('\n'); else System.out.write('\t'); System.out.write('\n'); ' \ t ' ' \ n ' c h a r ' \ t ' ' \ n 'i f if (i % 8 == 7) System.out.write(10); else System.out.write(9);
3 4 % 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 { ~ w r i t e () I O E x c e p t i o n t r y / c a t c h I O E x c e p t i o n try { for (int i = 32; i <= 127; i++) out.write(i); catch (IOException e) { System.err.println(e); I O E x c e p t i o n P r i n t S t r e a m S y s t e m. o u t w r i t e () I O E x c e p t i o n Writing Arrays of Bytes w r i t e ( ) public void write(byte[] data) throws IOException public void write(byte[] data, int offset, int length) throws IOException byte[] data d a t a o f f s e t l e n g t h s S y s t e m. o u t
3 5 String s = "How are streams treating you?"; byte[] data = s.getbytes(); System.out.write(data); w r i t e ( ) import java.io.*; public class AsciiArray { public static void main(string[] args) { byte[] b = new byte[(127-31)*2]; int index = 0; for (int i = 32; i < 127; i++) { b[index++] = (byte) i; // 8 if (i % 8 == 7) b[index++] = (byte) '\n'; else b[index++] = (byte) '\t'; b[index++] = (byte) '\n'; try { System.out.write(b); catch (IOException e) { System.err.println(e);
3 6 w r i t e () b y t e w r i t e () b y t e Flushing and Closing Output Stre a m s f l u s h ( ) public void flush() throws IOException f l u s h ( ) F i l e D e s c r i p t o r s y n c ( ) o u t o u t. f l u s h ( ) c l o s e ( ) public void close() throws IOException o u t o u t. c l o s e ( ) I O E x c e p t i o n
3 7 S y s t e m. o u t P r i n t S t r e a m S y s t e m. o u t I O E x c e p t i o n S y s t e m. o u t I O E x c e p t i o n S y s t e m. o u t t r y / c a t c h S y s t e m. o u t f l u s h () f l u s h () f l u s h () S y s t e m. o u t S y s t e m. e r r p r i n t l n () ' \ n ' P r i n t S t r e a m
3 8 O u t p u t S t re a m Subclassing OutputStre a m O u t p u t S t r e a m O u t p u t S t r e a m F i l e O u t p u t S t r e a m B y t e A r r a y O u t p u t S t r e a m b y t e O u t p u t S t r e a m w r i t e () public abstract void write(int b) throws IOException public void write(byte[] data) throws IOException public void write(byte[] data, int offset, int length) throws IOException O u t p u t S t r e a m write(int b) O u t p u t S t r e a m wr ite(byte [] data, int of fset, in t l e n g t h ) write(int b) public void write(byte[] data, int offset, int length) throws IOException { for (int i = offset; i < offset+length; i++) write(data[i]); write(byte[] data, int offset, int length) O u t p u t S t r e a m write(int b) write(data, 0, data.length) N u l l O u t p u t S t r e a m N u l l O u t p u t S t r e a m
3 9 package com.macfaq.io; import java.io.*; public class NullOutputStream extends OutputStream { public void write(int b) { public void write(byte[] data) { public void write(byte[] data, int offset, int length) { S y s t e m. o u t S y s t e m. e r r N u l l O u t p u t S t r e a m OutputStream out = new NullOutputStream(); PrintStream ps = new PrintStream(out); System.setOut(ps); System.setErr(ps); GUI A Graphical User Interface for Output Stre a m j a v a. a w t. T e x t A r e a S t r e a m T e x t A r e a S t r e a m e d T e x t A r e a t h e O u t p u t T e x t A r e a O u t p u t S t r e a m S t r e a m e d T e x t A r e a g e t O u t p u t S t r e a m () S t r e a m e d T e x t A r e a j a v a. a w t. T e x t A r e a
4 0 t h i s () s e t E d i t a b l e ( f a l s e ) T e x t A r e a a p p e n d ( ) s e t E d i t a b l e ( ) package com.macfaq.awt; import java.awt.*; import java.io.*; public class StreamedTextArea extends TextArea { OutputStream theoutput = new TextAreaOutputStream(); public StreamedTextArea() { this("", 0, 0, SCROLLBARS_BOTH); public StreamedTextArea(String text) { this(text, 0, 0, SCROLLBARS_BOTH); public StreamedTextArea(int rows, int columns) { this("", rows, columns, SCROLLBARS_BOTH);
4 1 public StreamedTextArea(String text, int rows, int columns) { this(text, rows, columns, SCROLLBARS_BOTH); public StreamedTextArea(String text, int rows, int columns, int scrollbars) { super(text, rows, columns, scrollbars); seteditable(false); public OutputStream getoutputstream() { return theoutput; class TextAreaOutputStream extends OutputStream { public synchronized void write(int b) { // int byte b &= 0x000000FF; // byte char append char c = (char) b; append(string.valueof(c)); public synchronized void write(byte[] data, int offset, int length) { append(new String(data, offset, length)); T e x t A r e a O u t p u t S t r e a m O u t p u t S t r e a m w t i t e () write(byte[] data, int offset, int length) g e t O u t p u t S t r e a m () w r i t e ()
4 2 S t r e a m e d T e x t A r e a