Output Stream Example

The only output streams you've seen so far are System.out and System.err. The following example uses the write() and flush() methods of the OutputStream class to write the String "Hello World" on System.out.

import java.io.*;


public class HelloOutputStream {

  public static void main(String[] args) {

    String s = "Hello World\r\n";

    // Convert s to a byte array
    byte[] b = new byte[s.length()];
    s.getBytes(0, s.length()-1, b, 0);
    try {
      System.out.write(b);
      System.out.flush();
    }
    catch (IOException ex) {
      System.err.println(ex);
    }

  }

}

What's wrong with this program?


Previous | Next | Top | Cafe au Lait

Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified August 18, 1997