使用BufferedReader和PrintWriter逐行复制文件
import java.io.FileReader; import java.io.FileWriter; import java.io.BufferedReader; /* n o w j a v a . c o m 提 供 */ import java.io.PrintWriter; import java.io.IOException; public class CopyLines { public static void main(String[] args) throws IOException { BufferedReader inputStream = null; PrintWriter outputStream = null; try {/* 来自 时 代 Java 公 众 号 - nowjava.com*/ inputStream = new BufferedReader(new FileReader("xanadu.txt")); outputStream = new PrintWriter(new FileWriter( "characteroutput.txt")); String l; while ((l = inputStream.readLine()) != null) { outputStream.println(l); } } finally { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } } } }