如何在Java应用程序中存储文件或文件路径?

时间:2023-01-13 21:14:50

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

如果这是一个非常初学的问题我很抱歉,但几年后我还没有使用过Java。

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).

在我的应用程序中,我需要跟上一个文件列表(大多数,如果不是全部,都是txt文件)。我需要能够添加到此列表,从列表中删除文件路径,并最终读取文件的内容(尽管不是在文件最初添加到列表时)。

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.

非常感谢。

5 个解决方案

#1


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

是的,路径通常存储为String或File实例。该列表可以存储为ArrayList实例。

#2


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
  • 如果您有少量文件和/或平面目录结构,则可以使用实现Collection的任何内容存储文件名/路径

  • if looking up files is performance critical you should use a data structure that gives you fast search, like a HashSet
  • 如果查找文件对性能至关重要,则应使用可快速搜索的数据结构,如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
  • 如果内存空间是一个问题(例如在移动设备上)并且您的文件数量很高和/或您的目录结构很深,那么您应该使用允许紧凑存储的数据结构,例如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.

如果数据结构允许,我会存储文件而不是字符串,因为没有额外的开销,文件显然提供了方便的文件处理方法。

#3


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.

一种方法是使用Properties类。它具有读取和写入文件的加载和存储方法,但它可能与您正在执行的操作不匹配。

#4


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.

我不确定我是否完全理解你的问题。但我喜欢将文件存储为Java中的文件对象。如果对每个文件应用相同的操作,则可以将它们存储在列表中。但也许你必须澄清一下你的问题。

#5


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.

我建议使用您选择的Collection接口存储一组文件对象。这样做的原因是文件对象创建了对文件的规范引用,该引用与设备无关。

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

#1


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

是的,路径通常存储为String或File实例。该列表可以存储为ArrayList实例。

#2


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
  • 如果您有少量文件和/或平面目录结构,则可以使用实现Collection的任何内容存储文件名/路径

  • if looking up files is performance critical you should use a data structure that gives you fast search, like a HashSet
  • 如果查找文件对性能至关重要,则应使用可快速搜索的数据结构,如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
  • 如果内存空间是一个问题(例如在移动设备上)并且您的文件数量很高和/或您的目录结构很深,那么您应该使用允许紧凑存储的数据结构,例如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.

如果数据结构允许,我会存储文件而不是字符串,因为没有额外的开销,文件显然提供了方便的文件处理方法。

#3


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.

一种方法是使用Properties类。它具有读取和写入文件的加载和存储方法,但它可能与您正在执行的操作不匹配。

#4


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.

我不确定我是否完全理解你的问题。但我喜欢将文件存储为Java中的文件对象。如果对每个文件应用相同的操作,则可以将它们存储在列表中。但也许你必须澄清一下你的问题。

#5


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.

我建议使用您选择的Collection接口存储一组文件对象。这样做的原因是文件对象创建了对文件的规范引用,该引用与设备无关。

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