File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes ArrayList of filenames Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "ArrayList of filenames" Watch "ArrayList of filenames" New topic
Author

ArrayList of filenames

Conrad McLaughlin
Greenhorn

Joined: Jan 31, 2006
Posts: 27
Is it possible to an arraylist (string) of the names of files within a directory? eg. textfiles

i'm guessing you would need a while loop. but how to use fileinputreader?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
In the java.io.File class there is a method called list() which will return a String array containing all the files and directories within the java.io.File if it is a directory.
Conrad McLaughlin
Greenhorn

Joined: Jan 31, 2006
Posts: 27
Thanks. I'm trying to work with it now.

but i keep getting:


Any tips on how to actually write the code to save filenames in string array?
Conrad McLaughlin
Greenhorn

Joined: Jan 31, 2006
Posts: 27
I solved the problem:


Thanks for the help.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Note that there is also a listFiles() method that returns an array of File objects. I often find that this is much more useful since I typically need File objects to deal with the files rather than just Strings representing their names. Also, it saves the step of creating my own File object for each String.

For more information, you should check out the Java API docs. These are always helpful to figure out the methods in a given class. Of course you have to know where to look, so that's why you should practice navigating them.

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: ArrayList of filenames
 
Similar Threads
readonly ArrayList
why some classes are called deprecated & some legacy?
Arraylist Vs LinkedHashMap
How do you avoid an ArrayList object from being modified, i.e. avoid adding and deleting its content
Collections.copy method for lists ? how it works