Estou executando a script abaixo para atualização em vários diretórios de uma máquina Sun.
Porém, se o número de diretórios for grande (+/- 1000), ocorre o erro após algumas execuções do comando:
java.io.IOException: Too many open files
Como fazer para ir “fechando” os processos e evitar o erro?
O que encontrei no Google foi:
“I suspect the problem is that the call to Runtime.exec() creates stream
handles for the standard input, output and error streams. You should
make sure to close these streams in your code. (Capture the Process
object returned from Runtime.exec() to get access to these streams.)”
Como faço isso?
Segue o código:
boolean recursive = true;
File root = new File ("C:/");
File pastaInicial = new File ("/inicial");
String cmd = "";
try {
if (root.exists()) {
File[] uf = root.listFiles();
for(int u = 0; u < uf.length; u++) {
if(recursive && uf[u].isDirectory()) {
File[] gestor = uf[u].listFiles();
for(int g = 0; g < gestor.length; g++) {
String nomeGestor = gestor[g].getName();
if (recursive && gestor[g].isDirectory()) {
File[] id = gestor[g].listFiles();
for (int i = 0; i < id.length; i++) {
if(recursive && id[i].isDirectory()) {
File pastaNova = new File (id[i].getPath() + "/nova");
cmd = "ln -s " + pastaInicial + " " + pastaNova;
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
}
catch (Exception e) {
out.println("Ocorreu erro: " + e.toString() + "<br>");
}
}
}
}
}
}
}
} else {
out.println("Pasta " + root +" não encontrada.<br>");
}
}
catch(Exception e) {
out.println("Ocorreu erro: " + e.toString());
}
Grato.
