[英]Java too many open files exception
我的代碼有問題; 基本上我有一個包含一些鍵的數組:
String[] ComputerScience = { "A", "B", "C", "D" };
依此類推,包含40個條目。
我的代碼從與ComputerScience的每個元素相對應的40個文件夾中讀取900 pdf,處理提取的文本並將輸出存儲在名為A.txt,B.txt,ecc的文件中。
每個文件夾“ A”,“ B”,ecc包含900 pdf。
在大量文檔之后,將引發異常“打開的文件太多”。 我想我正在正確關閉文件處理程序。
static boolean writeOccurencesFile(String WORDLIST,String categoria, TreeMap<String,Integer> map) {
File dizionario = new File(WORDLIST);
FileReader fileReader = null;
FileWriter fileWriter = null;
try {
File cat_out = new File("files/" + categoria + ".txt");
fileWriter = new FileWriter(cat_out, true);
} catch (IOException e) {
e.printStackTrace();
}
try {
fileReader = new FileReader(dizionario);
} catch (FileNotFoundException e) { }
try {
BufferedReader bufferedReader = new BufferedReader(fileReader);
if (dizionario.exists()) {
StringBuffer stringBuffer = new StringBuffer();
String parola;
StringBuffer line = new StringBuffer();
int contatore_index_parola = 1;
while ((parola = bufferedReader.readLine()) != null) {
if (map.containsKey(parola) && !parola.isEmpty()) {
line.append(contatore_index_parola + ":" + map.get(parola).intValue() + " ");
map.remove(parola);
}
contatore_index_parola++;
}
if (! line.toString().isEmpty()) {
fileWriter.append(getCategoryID(categoria) + " " + line + "\n"); // print riga completa documento N x1:y x2:a ...
}
} else { System.err.println("Dictionary file not found."); }
bufferedReader.close();
fileReader.close();
fileWriter.close();
} catch (IOException e) { return false;}
catch (NullPointerException ex ) { return false;}
finally {
try {
fileReader.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
但是錯誤仍然存在。 (它被拋出:)
try {
File cat_out = new File("files/" + categoria + ".txt");
fileWriter = new FileWriter(cat_out, true);
} catch (IOException e) {
e.printStackTrace();
}
謝謝。
編輯: 解決了我找到的解決方案,在調用writeOccurencesFile的主函數中,有一個函數創建了RandomAccessFile並且不關閉它。 調試器說在writeOccurencesFile中拋出了Exception,但是使用Java Leak Detector我發現在解析為純文本后pdf已經打開並且沒有關閉。
謝謝!
嘗試使用該工具專門為目的而設計的。
該Java代理是一個實用程序,可跟蹤JVM中打開文件的位置/時間/對象。 您可以讓代理跟蹤這些操作,以查找有關訪問模式或處理泄漏的信息,並轉儲當前打開文件的列表以及打開它們的位置/時間/對象。
發生異常時,此代理將轉儲列表,使您可以找出正在使用大量文件描述符的位置。
我已經嘗試過使用嘗試資源; 但問題仍然存在。 同樣在系統macOS內置控制台中運行的FileWriter fileWriter = ...
static boolean writeOccurencesFile(String WORDLIST,String categoria, TreeMap<String,Integer> map) {
File dizionario = new File(WORDLIST);
try (FileWriter fileWriter = new FileWriter( "files/" + categoria + ".txt" , true)) {
try (FileReader fileReader = new FileReader(dizionario)) {
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
if (dizionario.exists()) {
StringBuffer stringBuffer = new StringBuffer();
String parola;
StringBuffer line = new StringBuffer();
int contatore_index_parola = 1;
while ((parola = bufferedReader.readLine()) != null) {
if (map.containsKey(parola) && !parola.isEmpty()) {
line.append(contatore_index_parola + ":" + map.get(parola).intValue() + " ");
map.remove(parola);
}
contatore_index_parola++;
}
if (!line.toString().isEmpty()) {
fileWriter.append(getCategoryID(categoria) + " " + line + "\n"); // print riga completa documento N x1:y x2:a ...
}
} else {
System.err.println("Dictionary file not found.");
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
這是我現在使用的代碼,盡管對Exception的管理不善,為什么文件似乎沒有關閉?
現在我正在使用文件泄漏檢測器進行測試
也許您的代碼引發了另一個您未處理的異常。 嘗試在最后阻止之前添加catch(Exception e)
您也可以將BufferedReader聲明移出try並最后將其關閉
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.