编写一个 Java 应用程序,使用 Java 的输入输出流技术将 Input.txt 的内容(Input.txt 为文本 文件)逐行读出,
每读出一行就顺序为其添加行号(从 1 开始,逐行递增),并写入到另一 个文本文件 Output.txt 中
1 package asg6; 2 import java.io.BufferedReader; 3 import java.io.BufferedWriter; 4 import java.io.FileNotFoundException; 5 import java.io.FileReader; 6 import java.io.FileWriter; 7 import java.io.IOException; 8 public class ReadTxt { 9 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 BufferedReader br = null; 13 BufferedWriter bw = null; 14 try { 15 br = new BufferedReader(new FileReader("D:\大二\java\asg6\Input.txt")); 16 bw = new BufferedWriter(new FileWriter("D:\大二\java\asg6\Output.txt")); 17 String str = ""; 18 int i = 1; 19 while((str = br.readLine()) != null)//read a line 20 { 21 bw.write(i+" "); 22 bw.write(str); 23 System.out.println(str); 24 bw.newLine(); 25 i++; 26 } 27 bw.flush(); 28 bw.close(); 29 br.close(); 30 31 } catch (FileNotFoundException e) { 32 // TODO Auto-generated catch block 33 System.out.println("找不到指定文件!"); 34 }catch (IOException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } 38 } 39 40 }