簡體   English   中英

使用hashmap讀取csv文件; 沒有得到輸出

[英]reading csv file using hashmap; not getting the output

我的目錄中有多個csv文件。我想逐行讀取它們

使用hashmap iterator。從代碼開始讀取2個csv文件。稍后它將增加。

我已經寫了一些如下的代碼;

/**Read csv file line by line
 * 
 */
package com.sify.abcd;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class ReadCSV {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String csvFile = "C:/TestFileProgram/config";
        ReadCSV readCSV = new ReadCSV();
        readCSV.read(csvFile);


    }
    public void read(String fileName) {
        //Checks if it is a file
        File file = new File(fileName);
        if(file.isFile()){          
            if(file.getName().contains(".csv")){
                readCSV(file);
            }
        }
        //Checks if it is a directory
        else if (file.isDirectory()){
            System.out.println("Analysing Directory: "+file);
            String[] list = file.list();
            for (String innerFile : list) {
                read(file+"/"+innerFile);
            }
        }
    }
    public void readCSV(File cFile) {
        // TODO Auto-generated method stub
        BufferedReader br = null;
        String line = null;
        String csvSplitBy = ",";

        try {
            System.out.println("Reading file: "+cFile);
            Map<String, String> maps = new HashMap<String, String>();

            br = new BufferedReader(new FileReader(cFile));
            while ((line = br.readLine()) != null) {

                //use comma as separator
                String[] str = line.trim().split(csvSplitBy);

                for (int i = 0; i < str.length; i++) {
                    String string = str[i];
                    maps.put(string,line);
                }
            }
            //loop map
            for (Map.Entry<String, String> entry : maps.entrySet()) {
                System.out.println(entry.getValue() );

            }


        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        } 
        finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }    
    }

}

輸出為:

Analysing Directory: C:\TestFileProgram\com\config

Analysing Directory: C:\TestFileProgram\com\config\CSV

Reading file: C:\TestFileProgram\com\config\CSV\test_subtract_2.csv

test_subtract_2,subtract,0,0,0,0

Test Condition,Method,expected result,integer1,integer2,integer3

test_subtract_2,subtract,0,0,0,0

test_subtract_2,subtract,0,0,0,0

Test Condition,Method,expected result,integer1,integer2,integer3

Test Condition,Method,expected result,integer1,integer2,integer3

Test Condition,Method,expected result,integer1,integer2,integer3

Test Condition,Method,expected result,integer1,integer2,integer3

Test Condition,Method,expected result,integer1,integer2,integer3

Reading file: C:\TestFileProgram\com\config\TestAddNumbers.csv

Test Condition,Method,expected result,integer1,integer2,integer3

test_add_1`,add,0,0,0,

test_add_1`,add,0,0,0,

Test Condition,Method,expected result,integer1,integer2,integer3

Test Condition,Method,expected result,integer1,integer2,integer3

Test Condition,Method,expected result,integer1,integer2,integer3

Test Condition,Method,expected result,integer1,integer2,integer3

test_add_1`,add,0,0,0,

Test Condition,Method,expected result,integer1,integer2,integer3

Test Condition,Method,expected  result,integer1,integer2,integer3

是我的標題。所以我想先打印一次,在我的情況下,每個文件要打印多次。

test_add_1`,add,0,0,0,

最后還有一個逗號。 我不要那個。

請幫助解決此問題。

提前致謝...

這里:

for (int i = 0; i < str.length; i++) {
    String string = str[i];
    maps.put(string,line);
}

您將每個標記的每一行放置一次。 因此您的標頭有6個令牌,出現了6次。 由於您將令牌用作密鑰,因此test_subtract_2,subtract,0,0,0,0僅出現3次,因此每個不同的令牌都有一行。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM