Java I / O和HashMap

时间:2017-02-03 02:33:00

标签: java io hashmap

我有一个如下所示的文本文件:

A
Apple
B
Bat
C
Cat

...

我需要读取此文本文件并将其保存在HashMap中,其中奇数行是键,下面的偶数行是值。例如,(A,Apple)。我尝试使用以下代码,但它不起作用。有人可以给我一些关于如何做到这一点的暗示或建议吗?

     private HashMap<String, String> newHashMap = new HashMap<String, String>();

    Charset charset = Charset.forName("US-ASCII");

    Path path = Paths.get("file_location");
    try (BufferedReader reader = Files.newBufferedReader(path, charset)) {
        int lineCount = 0;
        String key;
        String value;

        String line = reader.readLine();
        while(line != null) {


            line = reader.readLine();

            if (lineCount % 2 == 1)
            {
            key = reader.readLine() ;

            }
            else if( lines % 2 == 0)
            {
            value = reader.readLine();

            }

            lineCount++;
           newHashMap.put(key, value);
        }

7 个答案:

答案 0 :(得分:2)

正如其他人所说,if语句和lineCount变量在这里是多余的。第一次调用readLine时也会导致问题,因为您实际上是在跳过文本文件中的第一行。你应该做的是在while循环中调用(line = r.readLine()) != null),这样你就可以访问循环中的读取行,同时还能够避免在文件结束后读取。此外,while循环line = reader.readLine();中的这一行也是不必要的。它导致您每次迭代读取一个额外的行,并跳过它,因为它从未使用过。除了读取while循环标题内的一行之外,只需读取while循环内的另一行,并将每行分配给正确的变量(keyvalue),如此,

while((line = reader.readLine()) != null) {
       key = line;
       value = reader.readLine();
       newHashMap.put(key, value);
}

并在while循环之外,将String line = reader.readLine();更改为

String line = "";

答案 1 :(得分:1)

您已经拥有keyvalue的变量,因此请使用intiially

    String key = reader.readLine();
    String value = reader.readLine();

    while(key != null && value != null) {
       newHashMap.put(key, value);
       key = reader.readLine();
       value = reader.readLine();
    }

答案 2 :(得分:1)

您的代码的主要问题似乎是您不知道调用bufferedReader.readLine()方法&#39; n&#39;时间会读到&#39; n&#39;线(如果可用)。有关详细信息,请参阅here。所以你的代码应该是这样的: -

String key = reader.readLine();

while(key!=null){
String value = reader.readLine();
newHashMap.put(key, value);
key = reader.readLine();
}

如果您的输入文件没有尾随键,则上述代码应该可以使用

答案 3 :(得分:0)

您的方法合理,只需移动newHashMap.put(key, value);块内的else if即可。在读完值后,您只想每隔一行添加到地图中。

答案 4 :(得分:0)

进行调整

else if( lines % 2 == 0) {
        value = reader.readLine();
        newHashMap.put(key, value); // update when you've pair of values(key + value)
}

然而,行

String line = reader.readLine();

使您跳过当前实现的第一行输入。

答案 5 :(得分:0)

  

如果你执行硬编码+2 while循环,你将无需使用   if语句同时能够直接关注   从每一行中提取数据。

上面的建议只是代码的“作弊”版本。我在下面改写了另一个版本。

首先,您已阅读第一行:

String line = reader.readLine();

如果我们删除它,

while((line = reader.readLine()) != null) {
           newHashMap.put(line, reader.readLine());
        }

我们会缩短代码。如果你看一下while循环,它实际上首先指定了行,所以我们在put方法中只有readLine才能获得偶数元素,在这种情况下是键的值。

简而言之,while循环携带关键数据,hashmap put方法的reader.readLine获取值数据。这很简单,您可以减少对更多内存地址的需求。

为了进一步加强我的答案,这是另一个溢出。 Using BufferedReader to read Text File

答案 6 :(得分:0)

您无需检查lineCount是奇数还是偶数。

Java Docs中的readLine()说明,

  

读取一行文字。一条线被认为是任何一条线终止的   换行(&#39; \ n&#39;),回车(&#39; \ r&#39;)或回车   然后立即换行。

因此,它一次读取一行。

您可以将代码修改为:

private HashMap<String, String> newHashMap = new HashMap<String, String>();

    Charset charset = Charset.forName("US-ASCII");

    Path path = Paths.get("file_location");

    try (BufferedReader reader = Files.newBufferedReader(path, charset)) {

          String key = reader.readLine();
          String value = reader.readLine();

           while(key != null && value != null) {
              newHashMap.put(key, value);
              key = reader.readLine();
              value = reader.readLine();
}

我在答案中借用了@Scary Wombat的code用户。他的代码在美学上更令人愉悦,并确保您不会意外地将null值插入HashMap。