views:

515

answers:

5

I apologize if this is a really beginner question, but I have not worked with Java in several years.

In my application, I need to keep up with a list of files (most, if not all, are txt files). I need to be able to add to this list, remove file paths from the list, and eventually read the contents of the files (though not when the files are initially added to the list).

What is the best data structure to use to store this list of files? Is it standard to just save the path to the file as a String, or is there a better way?

Thanks very much.

+3  A: 

Yes, paths are usually stored as String or File instances. The list can be stored as an ArrayList instance.

Vinay Sajip
Yes. If, within your Java app, you want to handle objects that represent files, surely the most natural way is bunch of File instances in a Collection.
Jonik
I vote for File objects. They don't actually open or create files, they just represent them, and come with helper methods to do things like check for existence, enumerate files in directories, etc.
Jason S
I agree with Jonik and Jason S. Use File over String. The additional functionality of File objects out weighs the overhead.
Daniel Nesbitt
I vote solution that Vinay and Jonik has, i.e. store as an arraylist of strings. When you need to get the File object, just create an instance with new File("some file location and name goes over here"). I just would like to add - use ArrayList<String> if you are in JDK 1.5 or later.
Roman Kagan
A: 

One way is to use the Properties class. It has load and store methods for reading and writing to a file, but it may not match what you are doing.

rado
I interpreted the question to be only about keeping and managing pointers to files in a Java app - not about persisting these pointers or anything
Jonik
Ah, I see, I agree that if you don't need to persist the paths then a simple ArrayList of Strings or File objects is better.
rado
A: 

I'm not sure if I understood your question completely. But I like to store Files as File Objects in Java. If you apply the same operation to each File then you can store them in a List. But maybe you have to clarify your question a little bit.

Janusz
+1  A: 

It really depends on your requirements

  • you can store filenames/paths using anything that implements Collection if you have a small number of files and/or a flat directory structure
  • if looking up files is performance critical you should use a data structure that gives you fast search, like a HashSet
  • if memory space is an issue (e.g. on mobile devices) and your number of files is high and/or your directory structure deep you should use a data structure that allows for compact storage, like a trie

If the data structure allows, I would store Files rather than Strings however because there is no additional overhead and File obviously offers convenient file handling methods.

Josef
Collection API link should probably point to http://java.sun.com/javase/6/docs/api/java/util/Collection.html?
Jonik
yep, my bad, thanks
Josef
A: 

I would recommend storing a set of file objects using the Collection interface of your choice. The reason to do this is that the File Object creates a canonical reference to the file, which is device independent.

I don't think that the handle is open when you do this, but I am open to correction.

http://java.sun.com/javase/6/docs/api/java/io/File.html

UberAlex