简体   繁体   English

读取和存储多个文件

[英]Reading and Storing Multiple Files

What I am trying to do is read from multiple files and then store the contents of the files into a data structure. 我想做的是从多个文件中读取文件,然后将文件的内容存储到数据结构中。 I will know how many files I have and the names of the files but the number and names of files can change. 我将知道我有多少个文件以及文件名,但是文件的数量和名称可以更改。 After I read in all the contents of the files and store them I need to be able to find a specific string that contains across all the files. 读完文件的所有内容并将其存储后,我需要能够找到包含所有文件的特定字符串。

What I am having trouble is I have no idea what will be the easiest data structure for this task. 我遇到的麻烦是我不知道该任务最简单的数据结构是什么。 I was thinking a hashmap, and using the file names as a key. 我在考虑一个哈希图,并使用文件名作为键。 Would that work? 那行得通吗? Is there a better data structure? 是否有更好的数据结构? I am using Java 我正在使用Java

Edit: I have been reading the contents of a file into an ArrayList because each line contains separate information that I need to be able to reference in the future. 编辑:我已经将文件的内容读取到ArrayList中,因为每一行包含单独的信息,以后我需要引用这些信息。

If your files are not big enough then you can use this simple Map 如果文件不够大,则可以使用此简单地图

       Map<String, List<String>> map = new HashMap<String, List<String>>();

key(String) as file name value(ArrayList) as contents of the file (lines) key(String)作为文件名value(ArrayList)作为文件内容(行)

If your files are so big then this data structure would not be sufficient. 如果文件太大,则此数据结构将不够用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM