使用hashmap读取csv文件;没有得到输出

时间:2014-03-25 06:57:37

标签: java csv iterator hashmap

我在目录中有多个csv文件。我想逐行读取它们

使用hashmap迭代器。由于代码正在读取2 csv file.later它会增加。

我已经编写了一些代码,如下所示;

/**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

The Line

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

是我的标题。所以我想首先打印它,只打印一次,在我的情况下,每个文件打印多次,也在这行中

test_add_1`,add,0,0,0,

最后有一个额外的commaa。我不希望这样。

请帮助解决这个问题。

提前致谢...

1 个答案:

答案 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只出现三次,因为您将该令牌用作关键字,因此每个不同的令牌都有一行。

希望这有帮助。