I have the code below, which simply reads all the files from a folder. There are 20,000 files in this folder. The code works good on a local folder (d:/files
), but fails on a network path (//robot/files
) after reading about 1,000 - 2,000 files.
Update: the folders are copies of each other.
What causes this problem and how to fix it?
package cef_debug;
import java.io.*;
public class Main {
public static void main(String[] args) throws Throwable {
String folder = args[0];
File[] files = (new File(folder)).listFiles();
String line;
for (int i = 0; i < files.length; i++) {
BufferedReader br = new BufferedReader(new FileReader(files[i]));
while ((line = br.readLine()) != null) {
}
br.close();
}
}
}
I get the following error when reading from a network path (//robot/files
):
Exception in thread "main" java.io.IOException: Too many open files
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileReader.<init>(FileReader.java:55)
at cef_debug.Main.main(Main.java:12)
Java Result: 1
Line 12 is the line:
BufferedReader br = new BufferedReader(new FileReader(files[i]));