簡體   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