2013-03-27 42 views
-1

我試圖通過添加java程序特別txt文件幾乎2lac線(實際上的conf文件)。但是,只有189000個人需要將近112分鐘! 我寫以下爲如何在最短時間內通過java在txt文件中添加2lac行?

import java.io.*; 
public class Fileshandling_example { 
    static long s1; 
    static long e1; 
    static long e2; 
    static Fileshandling_example fhe= new Fileshandling_example(); 
    public static void main(String args[]) { 
     try {   
      s1 = System.nanoTime();   
      File file1 = new File("\example\mandar.txt"); 
      LineNumberReader lnr1 = new LineNumberReader(new FileReader(file1)); 
      BufferedReader br1 = new BufferedReader(new FileReader(file1)); 
      lnr1.skip(Long.MAX_VALUE); 
      int a = 1; 
      StringBuffer sb1 = new StringBuffer("[stations]"); 
      String sCurrentline1 = br1.readLine(); 
      while ((sCurrentline1 = br1.readLine()) != null) { 
       a++; 
       if (sCurrentline1.contentEquals(sb1) == true) { 
        int count = a; 
        int arraycount = 100000; 
        for(int i =0; i< (arraycount+1); i++){ 
         if(0 == (i%10000)){ 
          e2 = System.nanoTime(); 
          System.out.println("Time = "+(e2-s1)); 
         } 
         String abc ="extern => 00"+(1000 + (arraycount-i))+",1,Wait(0.05)"; 
         fhe.insertintoExtensions(file1, (count+1),abc); 
        } 
       }  
      }    
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     e1 = System.nanoTime(); 
     System.out.println("Time = "+(e1-s1)); 
    } 
    public void insertintoExtensions(File inFile1, int lineno, String s1)throws Exception { 
     File outFile1 = new File("\example\111.tmp"); 
     FileInputStream fis = new FileInputStream(inFile1); 
     BufferedReader in = new BufferedReader(new InputStreamReader(fis));   
     FileOutputStream fos = new FileOutputStream(outFile1); 
     PrintWriter out = new PrintWriter(fos); 
     String thisLine = ""; 
     int i =1; 
     while ((thisLine = in.readLine()) != null) { 
      if(i == lineno) out.println(s1); 
      out.println(thisLine); 
      i++; 
     } 
     out.flush(); 
     out.close(); 
     in.close(); 
     inFile1.delete(); 
     outFile1.renameTo(inFile1); 

    } 
} 

代碼中的任何一個可以幫助我在那裏我得到錯誤的? 我問類似的問題coderanch但在這裏我得到的線索非常快,所以我在這裏也問。 對不起(交叉論壇問)。 謝謝。

+2

請您理解,* LAC *不是通常理解的單詞。你應該在這裏使用20萬。 – 2013-03-27 20:19:58

+0

你爲什麼要創建和刪除這麼多臨時文件? – pamphlet 2013-03-27 20:21:11

+0

[維基百科:十萬](http://en.wikipedia.org/wiki/Lakh) – 2013-03-27 20:21:28

回答

0

您是循環10萬次,每 '[站]' 上 「\例子\ mandar.txt」 發現:

if (sCurrentline1.contentEquals(sb1) == true) { 
    int count = a; 
    int arraycount = 100000; 
    for(int i =0; i< (arraycount+1); i++){ 

,並呼籲fhe.insertintoExtensions哪些循環「\例子\ mandar.txt 「再次複製或達到該行的內容或直至實際行號S1參數的內容:

while ((thisLine = in.readLine()) != null) { 
    if(i == lineno) out.println(s1); 
    out.println(thisLine); 
    i++; 
} 

儘量提高你的代碼並使用的BufferedWriter代替的PrintWriter。

+0

感謝DiogoSantana, 我想你的幫助下稱 http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/ 但我不能做得很好。你能幫助我嗎? – 2013-03-28 05:36:49

+0

當然。但你需要做更多的功課。調試你的代碼,想一個更好的方式來實現你的目標,當你被困住時,你可以問一個更具體的問題。 – DiogoSantana 2013-03-28 05:40:39

+0

你問爲什麼這段代碼很慢。答案就在那裏。如何使它快速,這只是一個改變代碼邏輯以避免100,000循環的問題。 – DiogoSantana 2013-03-28 05:41:44

相關問題