 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Pratik Guest
|
Posted: Tue May 16, 2006 6:07 am Post subject: Listing all the files and paths in a directory. |
|
|
I have this code which gets the absolute path and file name of all the
files in a directory and its subdirectories. I am trying to store these
values in a string array. The system.out.println() is printing all the
paths and files on the console but my string array is only grabbing the
last path name. I would like to store all the file names and path-names
in my array... Any help would be greatly appreciated..
(Mind you, I am using a recursive function)
public class ListAllFiles {
private static String[] pathList = new String[100];
private static String[] fileName = new String[100];
public static void visitAllDirsAndFiles(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
pathList[i] = children[i];
fileName[i] = dir;
visitAllDirsAndFiles(new File(dir, children[i]));
System.out.println(dir.getAbsolutePath());
System.out.println(children[i]);
}
}
}
public static void main(String[] args) throws IOException {
File dir = new File("c://sourceDirectory");
ListAllFiles.visitAllDirsAndFiles(dir);
for (int i=0; i<20; i++){
System.out.println(pathList[i]);
}
}
} |
|
| Back to top |
|
 |
Matt Humphrey Guest
|
Posted: Tue May 16, 2006 12:07 pm Post subject: Re: Listing all the files and paths in a directory. |
|
|
"Pratik" <pratik.pandey (AT) gmail (DOT) com> wrote in message
news:1147759014.861412.216070 (AT) y43g2000cwc (DOT) googlegroups.com...
| Quote: | I have this code which gets the absolute path and file name of all the
files in a directory and its subdirectories. I am trying to store these
values in a string array. The system.out.println() is printing all the
paths and files on the console but my string array is only grabbing the
last path name. I would like to store all the file names and path-names
in my array... Any help would be greatly appreciated..
(Mind you, I am using a recursive function)
public class ListAllFiles {
private static String[] pathList = new String[100];
private static String[] fileName = new String[100];
public static void visitAllDirsAndFiles(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
pathList[i] = children[i];
fileName[i] = dir;
visitAllDirsAndFiles(new File(dir, children[i]));
System.out.println(dir.getAbsolutePath());
System.out.println(children[i]);
|
You're going to have a number of problems with this code. First, using
arrays to store (pathList, fileName) the result means that you won't know
how many you actually collected and you may run out of space. Use Lists
instead. I would really suggest not using static variables because it means
you can only ever call your method once at any time. Rather, pass the list
being created as a parameter to your method. This will solve your real
problem, which is that when you recurse into your method, the loop starts
over again from 0. Each separate call to visitAllDirsAndFiles has its own
loop, but you are using i to index the output array, so you're always
starting from the beginning again. Using a method signature like below:
public static void visitAllDirsAndFiles (List foundFiles, File dir) {
}
Just change your method so that as you find each file, you add it to the end
of the list At the end you can step through the list to retrieve all the
items.(look up List / ArrayList / Iterator / size / get in the Java API.)
you would call it as:
List myList = new ArrayList ();
visitAllDirsAndFiles (myList, new File ("C://sourceDirectory");
Cheers,
Matt Humphrey matth (AT) ivizNOSPAM (DOT) com http://www.iviz.com/ |
|
| Back to top |
|
 |
Ed Kirwan Guest
|
Posted: Tue May 16, 2006 3:07 pm Post subject: Re: Listing all the files and paths in a directory. |
|
|
Pratik wrote:
| Quote: | I have this code which gets the absolute path and file name of all the
files in a directory and its subdirectories. I am trying to store these
values in a string array. The system.out.println() is printing all the
paths and files on the console but my string array is only grabbing the
last path name. I would like to store all the file names and path-names
in my array... Any help would be greatly appreciated..
(Mind you, I am using a recursive function)
public class ListAllFiles {
private static String[] pathList = new String[100];
private static String[] fileName = new String[100];
public static void visitAllDirsAndFiles(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
pathList[i] = children[i];
fileName[i] = dir;
visitAllDirsAndFiles(new File(dir, children[i]));
System.out.println(dir.getAbsolutePath());
System.out.println(children[i]);
}
}
}
public static void main(String[] args) throws IOException {
File dir = new File("c://sourceDirectory");
ListAllFiles.visitAllDirsAndFiles(dir);
for (int i=0; i<20; i++){
System.out.println(pathList[i]);
}
}
}
http://www.javapractices.com/Topic68.cjp |
--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
Download Fractality, free Java code analyzer:
www.EdmundKirwan.com/servlet/fractal/frac-page130.html |
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Tue May 16, 2006 8:07 pm Post subject: Re: Listing all the files and paths in a directory. |
|
|
On 15 May 2006 22:56:54 -0700, "Pratik" <pratik.pandey (AT) gmail (DOT) com>
wrote, quoted or indirectly quoted someone who said :
| Quote: | private static String[] pathList = new String[100];
|
A String[] is for when you know exactly how many elements you have.
When you don't know in advance, use an ArrayList<String>.
When the ArrayList is fully composed, you can convert it to a String[]
if you want.
see http://mindprod.com/jgloss/arraylist.html
http://mindprod.com/jgloss/array.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching. |
|
| Back to top |
|
 |
Pratik Guest
|
Posted: Wed May 17, 2006 3:07 am Post subject: Re: Listing all the files and paths in a directory. |
|
|
Matt,
Thank you very much for your post. I used ArrayList and then
passed it as a parameter to my function. That solved my problem.
cheers.. |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Jun 06, 2006 7:03 am Post subject: Re: Listing all the files and paths in a directory. |
|
|
Pratik wrote:
| Quote: | Matt,
Thank you very much for your post. I used ArrayList and then
passed it as a parameter to my function. That solved my problem.
cheers..
|
Interesting :)
-Thufir |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|