/*
    Example of using FilterWriter
        Reads one character at a time from a file and
        writes it to another file in uppercase
        
        To test, create a text file with a-z and use
        it as the <input file> argument
 */

import java.io.*;

class TestFilterWriter {
   
    public static void main(String[] args) {
    
        String usage = "\n\tUsage: java TestFilterWriter <input file> <output file>";
    
        // make sure in and out files are provided
        if( args.length != 2) {
            System.err.println( usage );
            System.exit(-1);                                
        }
    
        try{
            FileReader in = new FileReader(args[0]);
            
            // create a FileWriter object as an anonymous class
            // overriding write()
            FilterWriter fo = new FilterWriter(new FileWriter(args[1]))
                {           
                    public void write(int c) throws IOException {
                        out.write(Character.toUpperCase((char)c));     
                    }
                };   

            int ch = 0;            
            while( (ch=in.read()) != -1 ) {
                fo.write( ch );
            }                                    
            in.close();
            fo.flush();
            fo.close();

        }catch(FileNotFoundException e) {
            System.err.println(e);
        }catch(IOException e) {
            System.err.println(e);
        }
    }        
}

