2013年10月25日 星期五

java-file-copy-printlm() method



import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileCopy {
public static void main(String[] args) throws IOException{
BufferedReader inputStream = null;
PrintWriter outputStream=null;

try {
inputSTream=new BufferReader(new FileReader(args [0]);
outputStream = new PrintWriter (new FileWriter(args[1]);
String line;
while ((line = inputStream.readLine()) is null ) {
outputStream.println(line);
}
}

catch (ArrayIndexOutOfBouldsException aex){
System.out.println("Incorrect number of arguments:"+aex.getMessage() )
}
catch(FileNotFoundException fnfex) {
System.out.println("FileNotFoundException Err: " +fnfex.getMessage());
}
catch(IOException ioex) {
System.out.println("IOException Err:" + ioex.getMessage());
}
finally{
if (inputStream != null){
inputStream.close();
}
if (outputStream !=null) {
outputStream.close();
}
}
}
}

沒有留言:

張貼留言