The Java Program: Convert.java

  1 import java.io.*;
  2 
  3 public class Convert {
  4 
  5    public static void main (String[] args) throws IOException {
  6       final BufferedReader in;
  7       if (args.length>0) {
  8          in = new BufferedReader (new InputStreamReader (System.in, args[0]));
  9       } else {
 10          in = new BufferedReader (new InputStreamReader (System.in));
 11       }
 12 
 13       final PrintWriter out;
 14       if (args.length>1) {
 15          out = new PrintWriter (new OutputStreamWriter (System.out, args[1]));
 16       } else {
 17          out = new PrintWriter (new OutputStreamWriter (System.out));
 18       }
 19 
 20       /*
 21         The 16 bit value returned by read() represents a "code unit" in Unicode,
 22         the code units of the UTF-16 encoding as surrogates are not handled, this
 23         value does not fit in short (which is signed).
 24         See http://java.sun.com/j2se/1.50.docs/api/java/lang/Character.html#unicode
 25       */
 26       for (;;) {
 27           final int unichar = in.read();
 28           if (unichar == -1) break;
 29           out.print (unichar);
 30        }
 31        out.close();
 32        in.close();
 33     }
 34 }