简体   繁体   中英

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

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.

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)

If your files are so big then this data structure would not be sufficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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