【问题标题】:Inserting from Text document to Hashmap从文本文档插入到 Hashmap
【发布时间】:2018-11-30 04:46:11
【问题描述】:

您好,我正在尝试编写一个小程序来从文本文档中获取信息并将该信息插入到哈希图中。我不明白的棘手部分是如何区分信息,以便它进入我希望它在我的哈希图中的字段。

现在您可以说我将要使用的文本文档已经有了各种“标签”。例如,文本文档的信息显示格式如下:35=22, 20=0, 52=20180608-19:51:02.352, 56=ALPH

我正在考虑在 Hashmap 类型中插入所有这些信息。我只是想区分信息,例如:

BufferedReader reader = 
 new BufferedReader(newFileReader("C:\\Users\\darroyo\\Documents\\pruebasx.txt"));

String line=reader.readLine();
Map<Integer,String> hm1 = new HashMap<Integer,String>();
                    hm1.put(1, arg1)
                    hm1.put(2, arg1)
                    hm1.put(3, arg1)

这里 1 将读取标签 35 并放入值 22,hashmap 值 2 将读取标签 56 并放入值 ALPH。我怎样才能以这种方式识别信息?另外,如果我有多个格式相似的行,它们也会被传递到 hashmap 上,还是我必须创建一个新行?

【问题讨论】:

  • put(...)HashMap 方法不会“读取”任何内容。您需要逐行读取文件,以= 分隔输入,将第一个值(= 左侧)解释为键,将第二个值(= 右侧)解释为值。
  • 我假设分离将使用正则表达式完成?
  • String inputLine = ...; String[] values = inputLine.split("=");
  • 我认为您希望首先在 ',' 上拆分以获取 label=value 对,然后在 '=' 上拆分每个以隔离标签和值。

标签: java hashmap text-files bufferedreader


【解决方案1】:

我不确定您的文件是否可以包含行/换行符。如果没有,只需将while 语句与line = reader.readLine(); 交换即可。

Map<Integer,String> hm1 = new HashMap<Integer,String>();
String line;
// while there is a line to be read
while ((line = reader.readLine()) != null) {
    // we need to tokenize the line. the common separator is ", "
    // so that it what we shall split it by, to get an array of each value between ", "
    String[] tokens = line.split(",\\s"); //(in regex, \\s matches a whitespace such as a space)
    // if the space is unreliable, it can be made optional: line.split(",\\s?")
    // loop for each record
    for (int i = 0; i != tokens.length; i++) {
        // next we need to get the "data" portion of the record, which is after the '='
        /*
        ** we could use regex again
        ** i.e. tokens[i].split("=")[1] will be the "data"
        ** but as a general rule, if you can do it easily without regex, don't use regex.
        ** it might look innocent enough, but a lot goes on in a regex engine!
        **
        ** instead we just get the index of '=' in the string
        ** add 1 to move past it
        ** and then get the characters from that index onward
        ** i.e. tokens[i].substring(tokens[i].indexOf('=')+1)
        */
        //get the index at which the data starts in the string
        int dataIndex = tokens[i].indexOf('=') + 1;
        //get the chars from that index onward
        String data = tokens[i].substring(dataIndex);
        //insert it into the map
        hm1.put(new Integer(i),data);
    }
}

【讨论】:

  • 哇,我试过了,这正是我想要的,谢谢。
【解决方案2】:

您可以尝试使用Properties 类。

Properties pro = new Properties();
FileReader fr = new FileReader("C:\\Users\\darroyo\\Documents\\pruebasx.properties"); 
pro.load(fr);                    
Map<Integer,String> map = new HashMap<Integer,String>();
map.put(1,pro.getProperty(35));
map.put(2,prp.getProperty(56));

【讨论】:

  • 如果选择这种方式,必须使用properties文件的格式。
  • 属性文件的结构是“35=2”还是“35=”格式? ?
猜你喜欢
  • 1970-01-01
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多