FileDocCategorySizeDatePackage
StreamRecoder.javaAPI DocExample1446Wed Feb 15 04:54:54 GMT 2006None

StreamRecoder

public class StreamRecoder extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

      
    if (args.length < 2) {
      System.err.println(
       "Usage: java StreamRecoder "
        + "infile_encoding outfile_encoding infile outfile");
      return;
    }
    
    InputStreamReader isr = null;
    OutputStreamWriter osw = null;
    try {
      File infile = new File(args[2]);
      File outfile = new File(args[3]);
      
      if (outfile.exists()
        && infile.getCanonicalPath().equals(outfile.getCanonicalPath())) {
        System.err.println("Can't convert file in place");
        return;
      }
    
      FileInputStream fin = new FileInputStream(infile);
      FileOutputStream fout = new FileOutputStream(outfile);
      isr = new InputStreamReader(fin, args[0]);
      osw = new OutputStreamWriter(fout, args[1]);
      
      while (true) {
        int c = isr.read();
        if (c == -1) break;  // end of stream
        osw.write(c);
      }
      osw.close();
      isr.close();
    }
    catch (IOException ex) {
      System.err.println(ex);
      ex.printStackTrace();
    }
    finally {
      if (isr != null) {
        try {
          isr.close();
        } catch (IOException ex) {
          ex.printStackTrace();
        }
      }
      if (osw != null) {
        try {
          osw.close();
        } 
        catch (IOException ex) {
          ex.printStackTrace();
        }
      }
    }