試圖在大文件中查找單詞。文件逐行讀取。讀取redLine異常的方式時拋出。有沒有辦法解決這個問題?你可以在地板上看到它作爲一個字符串?讀取文件java.lang.OutOfMemoryError
for(String line; (line = fileOut.readLine()) != null;){
if(line.contains(commandString))
System.out.println(count + ": " + line);
count++;
}
java.lang.OutOfMemoryError:
UDP:
這是我所有的壞代碼:
static String file = "files.txt";
static String commandString = "first";
static int count = 1;
public static void main(String[] args) throws IOException
{
try(BufferedReader fileOut = new BufferedReader(new InputStreamReader(new FileInputStream(file), "Cp1251"))){
for(String line; (line = fileOut.readLine()) != null;){
if(line.contains(commandString))
System.out.println(count + ": " + line);
count++;
}
System.out.println("before wr close :" + Runtime.getRuntime().freeMemory());
fileOut.close();
}catch(Exception e) {
System.out.println(e);
}
}
這不應該是'for'循環。但無論如何,'fileOut'是什麼,它是如何定義和打開的?該文件來自哪裏,你確定它被正確地分解成行而不是數據/二進制文件? – RealSkeptic 2015-02-08 20:32:57
您應該向我們展示堆棧跟蹤以及與發生錯誤的行相關的支持代碼。您提供的循環顯示行字段被重複覆蓋,這不會導致OOM。 – MarsAtomic 2015-02-08 20:33:26
爲什麼在找到commandString時不打破for循環? – 2015-02-08 20:37:12