2012-06-06 37 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

您的代码可能会工作,因为您只是将每行加载到内存中。但是,一旦读取的数据量超过几百行,您将在标准输出缓冲区中丢失输出。

比较最好的做法是将多个项目加载到一个集合中,然后丢弃那些在完成时不需要的项目。这将保持内存使用率低。如果你想要聪明点,注意你的进程的内存使用情况,并在达到固定阈值时开始清理。