Example of building a list of file names?

R

Rhino

Can anyone point me to an example of Java code that searches for all the
files within a given Windows directory (and doesn't recurse into
subdirectories) and then stores the file names in some kind of array or
collection? For example, if my directory was c:\foo, it would store a list
of all the files found in the foo directory.

I've lost most of my old code examples due to a hard drive crash and feel
sure that this kind of code must have been done umpteen times before. I'll
write it from scratch if I have to but I'm hoping some kind soul can point
me to an example or just post it directly....
 
E

Eric Sosman

Can anyone point me to an example of Java code that searches for all the
files within a given Windows directory (and doesn't recurse into
subdirectories) and then stores the file names in some kind of array or
collection? For example, if my directory was c:\foo, it would store a list
of all the files found in the foo directory.

I've lost most of my old code examples due to a hard drive crash and feel
sure that this kind of code must have been done umpteen times before. I'll
write it from scratch if I have to but I'm hoping some kind soul can point
me to an example or just post it directly....

String[] names = new File("c:\\foo").list();

I hope your new drive has enough room for this.
 
R

Roedy Green

Can anyone point me to an example of Java code that searches for all the
files within a given Windows directory (and doesn't recurse into
subdirectories) and then stores the file names in some kind of array or
collection? For example, if my directory was c:\foo, it would store a list
of all the files found in the foo directory.

see http://mindprod.com/products1.html#COMMANDLINE
--
Roedy Green Canadian Mind Products
http://mindprod.com

You can’t have great software without a great team, and most software teams behave like dysfunctional families.
~ Jim McCarthy
 
R

Rhino

Eric Sosman said:
Can anyone point me to an example of Java code that searches for all the
files within a given Windows directory (and doesn't recurse into
subdirectories) and then stores the file names in some kind of array or
collection? For example, if my directory was c:\foo, it would store a
list
of all the files found in the foo directory.

I've lost most of my old code examples due to a hard drive crash and feel
sure that this kind of code must have been done umpteen times before.
I'll
write it from scratch if I have to but I'm hoping some kind soul can
point
me to an example or just post it directly....

String[] names = new File("c:\\foo").list();

I hope your new drive has enough room for this.

I'm _REALLY_ rusty on my Java - haven't touched it in almost 4 years and I
feel like I've forgotten almost everything. For some reason, I thought that
was going to be more involved that it was.

Thanks, Eric!
 
E

Eric Sosman

Eric Sosman said:
Can anyone point me to an example of Java code that searches for all the
files within a given Windows directory (and doesn't recurse into
subdirectories) and then stores the file names in some kind of array or
collection? For example, if my directory was c:\foo, it would store a
list
of all the files found in the foo directory.

I've lost most of my old code examples due to a hard drive crash and feel
sure that this kind of code must have been done umpteen times before.
I'll
write it from scratch if I have to but I'm hoping some kind soul can
point
me to an example or just post it directly....

String[] names = new File("c:\\foo").list();

I hope your new drive has enough room for this.

I'm _REALLY_ rusty on my Java - haven't touched it in almost 4 years and I
feel like I've forgotten almost everything. For some reason, I thought that
was going to be more involved that it was.

One thing you appear to have forgotten in the existence
of something called "Javadoc" ...

(Another is that quoting Usenet signatures is frowned
upon. Have a care, or Lew will chastise you!)
 
R

Roedy Green

String[] names = new File("c:\\foo").list();

that is a fairly small list. list does not recurse for you.
--
Roedy Green Canadian Mind Products
http://mindprod.com

You can’t have great software without a great team, and most software teams behave like dysfunctional families.
~ Jim McCarthy
 
E

Eric Sosman

String[] names = new File("c:\\foo").list();

that is a fairly small list. list does not recurse for you.

The O.P. asked for "an example of Java code that searches
for all the files within a given Windows directory (and doesn't
recurse into subdirectories)."

Harrumph.
 
A

Arne Vajhøj

String[] names = new File("c:\\foo").list();

that is a fairly small list. list does not recurse for you.

The O.P. asked for "an example of Java code that searches
for all the files within a given Windows directory (and doesn't
recurse into subdirectories)."

Harrumph.

Reading what he comment on does not seem to be Roedy's
strong side.

Arne
 
R

Roedy Green

The O.P. asked for "an example of Java code that searches
for all the files within a given Windows directory (and doesn't
recurse into subdirectories)."

Harrumph.

Sorry about that. I saw the word "recurse" and thought -- he needs
help with recursion. I could see someone asking for help with
recursion but not with File.list which is completely straightforward.

Most newbie questions to me read like gibberish, so I tend just to
skim them, even when they are coherent. I pick out a few keywords and
guess what the user is actually asking. Most of you are much better at
interpreting them literally.

I am more interested in the general audience than the particular
newbie, so it is not of paramount importance that I answer their
question specifically, just that I put out something useful on the
general topic. I figure a newbie asking a particular question is
probably lacking in background. With background, they can often
answer their own question. I generally frown on spoon feeding. I
like to make the newbie stretch a bit.
 
A

Arne Vajhøj

Sorry about that. I saw the word "recurse" and thought -- he needs
help with recursion. I could see someone asking for help with
recursion but not with File.list which is completely straightforward.

Most newbie questions to me read like gibberish, so I tend just to
skim them, even when they are coherent. I pick out a few keywords and
guess what the user is actually asking.

That is not particular respectful for the people writing and reading
here.
Most of you are much better at
interpreting them literally.

I am more interested in the general audience than the particular
newbie, so it is not of paramount importance that I answer their
question specifically, just that I put out something useful on the
general topic. I figure a newbie asking a particular question is
probably lacking in background. With background, they can often
answer their own question. I generally frown on spoon feeding. I
like to make the newbie stretch a bit.

Well - in this case you did not reply to a newbie but to Eric.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top