我一直坚持这个项目,它要求我创建一个类来读取多个 txt 文档并将它们显示在我的主应用程序的文本区域上。
文档采用这种通用格式 id<>名称
需要拆分<>,文本区域中只显示名称。我的老师说 HashMap 将是一个很好使用的集合,但整个概念对我来说有点模糊。
我需要帮助创建一个处理 I/O + hashmap 的类来存储所有文件,然后能够显示主应用程序的名称部分。非常感谢您的帮助!
最佳答案
使用 BufferedReader 从文件中读取每一行,然后按照下面的伪代码进行操作(因为这是家庭作业,我无法提供实际的代码)。您基本上正在寻找 String.split(...)
、BufferedReader
、FileReader
、Map
(HashMap) 类。
第 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)
}
第 2 步
//Display each entry from the map
for each entry in the Map
display the value in text area
关于用于将 TXT 读入 hashmap 的 java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9459013/