Reading a file or reading multiple files from a (single)directory is such a easy task in Java. As every Java programmers know how use the method listFiles() to achieve the above mentioned task. Headache comes only when you try to read multiple files from multiple directory.
Well in this post I document my simple logic to read Multiple files from From Multiple directory. The pictorial representation of reading multiple files from single directory is show below.
Usual logic followed by most Java programmer to read multiple files is
Now lets make the scenario bit complicated that is reading multiple file from multiple directory. The pictorial representation is shown below.
The above diagram show that the main folder named ABC contain sub-folder A,B,C and each sub-folder contain multiple files that has to be read.
Consider little more complicated scenario that is a sub-folder containing multiple files along with another folder which has got another set of multiple files that has to be read. Simplified pictorial representation is shown below.
So for above case reading process should traversal to A.1 from A also then to Folder B and so on …
Simplest way to resolve the above two cases(2 & 3) is get all the directories on first traversal then put those directories in an array along with their path. Then repeat case 1 step for each directory in array. This could not be the solution if no of directories (sub-folder or) is large in number and you need to have parallel logic to handle sub-sub-folders which may increase the process cycle exponentially.
Finally here I propose my simple logic to solve all the above cases. Technique behind the logic is nothing superficially , I just kept the reading file process and directory traversal process separately by means by function. This help me to keep track of traversal with minimum process cycles.
5 Comments
at 5:29 AM - 26th August 2011 Permalink
thank u soo much the code which u gave was of great help for me
at 4:04 PM - 26th April 2012 Permalink
This code can be better. You reference a class ReadFile and don’t even discuss what it is. Change that please. Further, your first paragraph of codes has errors. Please see your loop statement.
at 4:33 PM - 26th April 2012 Permalink
Thats really rude.
Above snap shots give glims of code and not full the source, first undestand that.
Post is to demonstrate the logic and not code.
at 12:11 PM - 2nd February 2015 Permalink
Can u send me the Full code
at 12:12 PM - 2nd February 2015 Permalink
er.arpit111@gmail.com
Post a Comment