繁体   English   中英

Java I / O和HashMap

[英]Java I/O and 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);
        }

正如其他人所说,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 = "";

您已经具有用于keyvalue变量,因此请先使用

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

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

您的代码的主要问题似乎是您不知道调用bufferedReader.readLine()方法'n'次会读取'n'行(如果可用)的事实。 有关更多详细信息,请参见此处 因此,您的代码应如下所示:-

String key = reader.readLine();

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

如果您的输入文件没有尾随键,则上面的代码应该可以正常工作

您的方法是合理的,只需移动newHashMap.put(key, value); else if块内。 读取值后,您只想每隔两行将其添加到地图中。

要做一个调整

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();

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

如果执行+2硬编码的while循环,则无需使用if语句,同时可以直接专注于从每一行提取数据。

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

首先,您已经阅读了第一行:

String line = reader.readLine();

如果我们删除它,

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

我们将缩短代码。 如果您查看while循环,它实际上首先分配了该行,因此,通过获取even元素(在本例中为键的值),我们在put方法中仅具有readLine。

简而言之,while循环携带关键数据,hashmap put方法中的reader.readLine获得值数据。 如此简单,您就减少了占用更多内存地址的需求。

为了进一步加强我的答案,这是另一个溢出。 使用BufferedReader读取文本文件

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

Java Docs中的readLine()的描述说

读取一行文本。 一行被认为由换行符('\\ n'),回车符('\\ r')或回车符后立即换行符中的任何一个终止。

因此,它一次读取一行。

您可以将代码修改为:

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的代码 他的代码在美学上更令人愉悦,并确保您不会在HashMap中意外插入null值。

暂无
暂无

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

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