將一個文本文件的內容按行讀出,每讀出一行就順序加上行號,並寫入到另一個文件中。


Scanner類的nextLine方法不僅可以從鍵盤接收數據,也可以從文件讀取數據,且都是以回車為分隔.

 1 public class SY64 {
 2     public static void main(String[] args) throws Exception {
 3         int lineNumber=0;
 4         File f1 = new File("d:\\test.txt");
 5         File f2 = new File("d:\\test2.txt");
 6         PrintStream ps = new PrintStream(f2); 
 7         Scanner in = new Scanner(f1);
 8         String st = null;
 9         while(in.hasNext()){
10             st = in.nextLine();
11             ps.println((++lineNumber) + st);
12         }
13         ps.close();
14         in.close();
15     }
16 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM