使用BufferedReader对象的readLine()方法必须处理java.io.IOException异常(Exception).
一次可以读一行:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
public class ConsoleOutput {
public static void main(String arg[]) {
if(arg.length != 2) {
System.out.println("Usage: java CopyTextLines <infile> <outfile>");
return;
}
try {
String str;
FileReader fr = new FileReader(arg[0]);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(arg[1]);
BufferedWriter bw = new BufferedWriter(fw);
while((str = br.readLine()) != null)
bw.write(str);
br.close();
bw.close();
} catch(IOException e) {
System.out.println(e);
}
}
}
本文转自gnuhpc博客园博客,原文链接:http://www.cnblogs.com/gnuhpc/archive/2012/12/17/2822308.html,如需转载请自行联系原作者