The problem: I need to ensure that a particular directory hierarchy exists in the file system, and create it if it doesn’t.
I know that there is the method File.mkdirs() however the javadocs for that method indicate that it could fail creating some or all of the directories necessary to complete the path given.
To get around that, I made a method that takes a String representing the path and parses it into the individual parts, storing them into an ArrayList<String>. A second method would then go through and build the directories up, making sure that each directory and sub directory existed.
The problem is, the actual program runs out of memory when trying to loop through and break up the string into its individual parts. I know it’s not the function; it works perfectly on its own in a JUnit test, and the program itself isn’t very memory intensive. At this point, it’s just set directories up and stored them into a “DataBank” class – nothing big, just a class storing a bunch of strings.
I’d like to figure out what’s causing the problem; I can reply with the different classes in effect at the point the code stops.
However, a simpler solution might just be to use File.mkdirs(). What would cause File.mkdirs() to not create directories? Would it just be insufficient permissions?
I’d like to simplify the process if I can, instead of trying to re-invent the wheel. At the same time, though, I know that the program should not be running out of memory – I’m storing Strings and performing file operations, nothing super intensive.
File.mkdirs()could fail for any of the reasons thatFile.mkdir()fails (except for failing due to the parent directory not existing (for obvious reasons)).From the Javadoc:
The bold bit is a clue that
mkdirs()is just a shortcut for multiple calls tomkdir().Here’s what
mkdirs()does – it effectively callsmkdir()up the directory hierarchy: