简体   繁体   English

将TXT读入hashmap的java类

[英]java class for reading TXT into hashmap

I've been stuck on this project where it asks me create a class to read multiple txt documents and display them on my main app's textarea. 我一直在这个项目上工作,它要求我创建一个类来读取多个txt文档并将它们显示在主应用程序的textarea上。

the documents are in this general format id<>name 这些文档采用此通用格式id <> name

The <> needs to be split, and only the name should be displayed in the textarea. 需要分隔<>,并且仅应在文本区域中显示名称。 My instructor said hash maps would be a good collection to use, but this entire concept is kinda blurry to me. 我的老师说,散列图将是一个很好的集合,但是整个概念对我来说有点模糊。

I need help creating a class that handles I/O + hashmap to store all files, then be able to display the name part for the main app. 我需要帮助来创建一个处理I / O +哈希图的类,以存储所有文件,然后能够显示主应用程序的名称部分。 Help is much appreciated! 非常感谢帮助!

Use BufferedReader to read each line from the file and then follow the below pseudo code (Since this is homework I can't provide the actual code). 使用BufferedReader从文件中读取每一行,然后遵循以下伪代码(由于这是家庭作业,因此我无法提供实际代码)。 You basically looking for String.split(...) , BufferedReader , FileReader , Map (HashMap) classes. 您基本上是在寻找String.split(...)BufferedReaderFileReaderMap (HashMap)类。

Step 1 第1步

//Read each file in to Map
for each line
{
  split the line at <>
  you will have two tokens
  token 1 is id and token 2 is the name
  store both the tokens in Map (token 1 is the key and token 2 is the value)
}

Step 2 第2步

//Display each entry from the map
for each entry in the Map
display the value in text area

As this is a homework, I can only provide some direction to solve the problems. 因为这是家庭作业,所以我只能提供一些解决问题的指导。

1) You have multiple documents all containing multiple line with each line having format id<>name. 1)您有多个文档,每个文档都包含多行,每行的格式为id <> name。

2) You can read file using Java File I/O API 2)您可以使用Java File I / O API读取文件

3) Create hashmap 3)创建哈希图

4) Read each document file line by line 4)逐行读取每个文档文件

4) Split the line using String.split("<>"), you will get two strings id and name 4)使用String.split(“ <>”)拆分行,您将获得两个字符串ID和名称

5) use ID as a key and name as a value. 5)使用ID作为键,并使用名称作为值。 Put this in hashmap 把它放在哈希图中

6) After parsing all documents you will have filled hashmap 6)解析所有文档后,您将填充哈希图

7) Use java swing API for TextArea to display content hashmap in TextArea. 7)使用Java Swing API for TextArea在TextArea中显示内容哈希图。

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

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