2015年10月7日 星期三

I/O Chain 的練習

Buffer 與 File 的 Chain :
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ChainDemo {
    public static void main(String[] args){
        try(BufferedReader bufInput = new BufferedReader(new FileReader(args[0]));
            BufferedWriter bufOutput = new BufferedWriter(new FileWriter(args[1]))){
            String line = "";
            while ((line = bufInput.readLine()) != null){
                bufOutput.write(line);
                bufOutput.newLine();
            }
        }   catch (FileNotFoundException ex) {
            System.out.println("File not found!");
        } catch (IOException ex) {
            System.out.println("I/O Error");
        }  
    }
}