User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » Reading in all files below a directory in Java Page [1]  
neolithic
All American
706 Posts
user info
edit post

I want to be able to read in all files below a certain directory level and whose names are unkown. I actually know them but don't want to code in 100+ file names. Ideas?

12/10/2005 2:14:46 AM

marilynlov7
All American
650 Posts
user info
edit post

Haha would involve changing the names of your files but what if you renamed them to something like


file001
file002
file003


That way you could just write a loop to open them up.

OR

I just found this::


// Process all files and directories under dir
public static void visitAllDirsAndFiles(File dir) {
process(dir);

if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
visitAllDirsAndFiles(new File(dir, children[i]));
}
}
}

// Process only directories under dir
public static void visitAllDirs(File dir) {
if (dir.isDirectory()) {
process(dir);

String[] children = dir.list();
for (int i=0; i<children.length; i++) {
visitAllDirs(new File(dir, children[i]));
}
}
}

// Process only files under dir
public static void visitAllFiles(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
visitAllFiles(new File(dir, children[i]));
}
} else {
process(dir);
}
}

from here: http://javaalmanac.com/egs/java.io/TraverseTree.html

That might work for you too...

[Edited on December 10, 2005 at 10:32 AM. Reason : found more info...]

12/10/2005 10:22:09 AM

neolithic
All American
706 Posts
user info
edit post

thanks man, i'll try that out

12/10/2005 2:36:41 PM

qntmfred
retired
40816 Posts
user info
edit post

yeah, no need to rename anything

google search for recursive directory listing in java should be more than sufficient

12/10/2005 8:55:03 PM

LittleZZ
Veteran
442 Posts
user info
edit post

maybe i'm not understanding the question, but couldn't you just use a java File object like this:

File myDirectory = new File(directory);
File[] files = myDirectory.list();

if there are directories within that directory and you want to get those you'd have to loop through the array, check if it is a directory (using isDirectory()), and then get the contents for each one. If you just need the names than you can just set the list to an array of Strings instead of Files like:

String[] files = myDirectory.list();

Just google "java File" and look at the javadoc for more info

12/10/2005 9:50:02 PM

skokiaan
All American
26447 Posts
user info
edit post

are you actually a junior in csc/cpe?????


w

t

f

12/10/2005 10:16:31 PM

Perlith
All American
7620 Posts
user info
edit post

^^^
Doing it recursively can break sometimes

^
I was a junior by credit hours my third semester and didn't have many classes under my belt by that point. Please contribute something useful


neo, I don't think many of us undertand what you are trying to do. Are you trying to get the names of the files, read the contents of the files into memory, or what? "Below a certain directory" ... are you talking about "subdiretory depth of 5" or "/" or what?

12/11/2005 12:33:32 PM

State409c
Suspended
19558 Posts
user info
edit post

just use perl

12/11/2005 12:52:58 PM

LimpyNuts
All American
16859 Posts
user info
edit post

The question is straightforward and the answer is not that omplicated either. I don't know much java but I've done exactly this in several languages. Heres the process:

Define the starting point:
1. let "BaseDir" be that point (i.e. "C:\MyApp\loadthesefiles")

Create a global array (one that can be accessed from any subroutine) for file names;
2. let "FileArray[]" be that array

Create a recursive subroutine or function. It should take a directory as an input:
3. let "FileList(Directory)" represent that function
3.a. this subroutine should open the indicated directory and read its contents (see LittleZZ's post)
3.b. loop through the directory's contents checking each item to see if it's a file or a directory
3.b.i. if it's a file, append it's path and name to FileAray[]
3.b.ii. if it's a directory, have FileList call itself on that directory like "FileAray(files[i])"

4. Call FileList(BaseDir)
5. Upon completion, FileArray[] will contain every file name under your base directory

Java should have a big enough stack to go at least dozens of directories deep. VB's (ver 6) stack size is 128. Directories can be nested 255 levels deep. If you need o go deeper than stak space allows, this routine (and all recursive routines) can be writen as a flat code (i.e. no function calls). All you need to do in that case is track a queue of directories to be searched in an array and process each directory entirely before moving to the next.

12/11/2005 1:06:52 PM

qntmfred
retired
40816 Posts
user info
edit post

Quote :
"Doing it recursively can break sometimes"


anything can break sometimes if you do it wrong

12/11/2005 1:56:22 PM

skokiaan
All American
26447 Posts
user info
edit post

This is csc 116 shit.

12/11/2005 3:01:40 PM

GenghisJohn
bonafide
10252 Posts
user info
edit post

^^^^

12/11/2005 3:30:04 PM

skokiaan
All American
26447 Posts
user info
edit post

System.exec( perl get me tha files);

[Edited on December 11, 2005 at 3:31 PM. Reason : dsa]

12/11/2005 3:31:30 PM

philihp
All American
8349 Posts
user info
edit post

^that presupposes perl is on the machine.

12/12/2005 8:51:36 AM

 Message Boards » Tech Talk » Reading in all files below a directory in Java Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.