2012-06-06 45 views
1

如何讀取一個TB大小的兩個日誌文件,而不會耗盡我的機器上的內存。我會對它們進行一些比較。我想要做的這在Java.Would下面的代碼工作嗎?我擔心的是,FileStream將無法保存日誌文件的數據。從日誌文件中讀取一個TB的數據

public static void main(String args[]) 
{ 
    try{ 
    // Open the file that is the first 
    // command line parameter 
    FileInputStream fstream = new FileInputStream("textfile.txt"); 
    // Get the object of DataInputStream 
    DataInputStream in = new DataInputStream(fstream); 
    BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
    String strLine; 
    //Read File Line By Line 
    while ((strLine = br.readLine()) != null) { 
     // Print the content on the console 
     System.out.println (strLine); 
    } 
    //Close the input stream 
    in.close(); 
    } 
    catch (Exception e){//Catch exception if any 
    System.err.println("Error: " + e.getMessage()); 
    } 
} 

任何人都可以指導我這樣做的正確方法。

+0

您是否試過該代碼?這是功課嗎? – hatchet

+7

運行它,讓我們知道它是如何去。如果/當它失敗*然後*適當的問一個關於它的問題。 – Crisfole

+0

什麼是*實際*問題?這是否是一種「正確」的方式?它會工作 - 但你會輸出一些數據到控制檯。 –

回答

3

您的代碼可能會工作,因爲您只是將每行加載到內存中。但是,一旦讀取的數據量超過幾百行,您將在標準輸出緩衝區中丟失輸出。

比較最好的做法是將多個項目加載到一個集合中,然後丟棄那些在完成時不需要的項目。這將保持內存使用率低。如果你想要聰明點,注意你的進程的內存使用情況,並在達到固定閾值時開始清理。