0

I have a working tree which is the following:

Root
├─Site1
| ├─file.def
| ├─subfolder1
| └─subfolder2
├─Site2
| ├─file.def
| ├─subfolder1
| └─subfolder2
└─Site3
  └...

there are thousands of files contained in the subfolders. Now, I want to read the file.def, which contains data about site's subfolders and then open the subfolders according to said data. I'm pretty sure there is a better way to do that but here's my solution:

Collection<File> DefFiles = FileUtils.listFiles("C:/Root", acceptmapDef, true); //listFiles Accepting def files only
Collection<File> Otherfiles = FileUtils.listFiles("C:/Root"), acceptMapOthers, true); //listFiles Accepting every other file

for(Files def: DefFiles){
// Read def file
  for(Files file: OtherFiles){
     // creates a JSONObject from the correspondance between the def file 
     // and the html files found in the subfolders
    }
}

Now, when I run my script on unix, after some time the process obviously crashes and the "too many files open" exception is thrown. IMO, the fact that the script reads all the files twice is a problem, but since I'm new to fileUtils, that's the only way I found. Any idea would be appreciated

  • 2
    You don't need to list all files at once: you can iterate over `.def` files, and for each one list all files in same directory, opening them separately and *closing as needed*. You didn't show what you actually *do* with your files, but usually error you're getting doesn't come from just listing files - it usually happens when you forget to close previously opened ones. – M. Prokhorov Aug 23 '17 at 14:08
  • 1
    Do you need to close files in a foreach? If yes, how to you do that? – Jeremy Comelli Aug 23 '17 at 14:11
  • "How" depends on what you're doing with files: you don't show us that. – M. Prokhorov Aug 24 '17 at 09:27

0 Answers0