文件IO期间的内存溢出

时间:2012-06-11 13:44:40

标签: java memory-management file-io

在我暑期实习期间,我们有一个程序,每5分钟ping一次我们所有的Intranet网站,以确保它们仍在运行。我们还有每次输入网站时记录的日志文件。我必须编写一个程序,它将从站点监视器中删除日志中的条目,以便我们可以在其上运行我们的压缩脚本。

这是我的计划。有没有更好的方法来做到这一点,所以我在写回文件之前没有保存整个文件?

public class Scrubber {

String fileName;

String alertPhrase;

private ArrayList<String> fileContents;

public Scrubber(String fileName, String alertPhrase){
    this.fileName = fileName;
    this.alertPhrase = alertPhrase;
    fileContents = new ArrayList<String>();
}

public void ReadFile() throws FileNotFoundException{
    boolean containsAlertPhrase = false;    // flag to check for alertPhrase
    File file = new File(fileName);

    //check to see if its a file or directory being looked at
    if(file.isFile() != false){         
        Scanner fileIn = new Scanner(new FileReader(fileName));
        while(fileIn.hasNextLine()){
            String temp = fileIn.nextLine();        //copy the line to a temp String

            //check the temp String for the alert phrase
            containsAlertPhrase = temp.indexOf(alertPhrase) != -1;      

            //if the alertPhrase is not in the line, add it to the arraylist
            if(!containsAlertPhrase){
                fileContents.add(temp);
            }
        }
    } else {
        //not a file
    }       
}

public void WriteFile() throws FileNotFoundException{
    PrintWriter fileOut = new PrintWriter(fileName);

    //iterate through the arraylist and write each entry as its own line in the file
    for(String s : fileContents){
        fileOut.println(s);
    }
    fileOut.flush();
    fileOut.close();
}

}

此程序正在运行Windows Server 2003的服务器上运行。如果我无法编写更高效的程序,如何增加服务器上的堆大小?

谢谢!

0 个答案:

没有答案