编写一个Java应用程序,使用Java的输入输出流技术将Input.txt的内容逐行读出,每读出一行就顺序为其添加行号(从1开始,逐行递增),
并写入到另一个文本文件Output.txt中。
import java.awt.geom.Path2D;
import java.io.*;
import org.omg.PortableInterceptor.USER_EXCEPTION;
public class Exam3Test8 {
public static void main(String[] args) throws Exception {
appendLineNumber(new File("D:/input.txt"), new File("D:\\output.txt"));
}
public static void appendLineNumber(File from, File to) throws Exception {
BufferedReader in = new BufferedReader(new FileReader(from));
StringBuilder sb = new StringBuilder();
String t;
int lineNumber = 1;
while ((t = in.readLine()) != null) {
sb.append(lineNumber + " ");
sb.append(t);
sb.append(System.getProperty("line.separator"));
lineNumber++;
}
in.close();
BufferedWriter out = new BufferedWriter(new FileWriter(to));
out.write(sb.toString());
out.close();
}
} |
|