【问题标题】:reading csv file using hashmap; not getting the output使用 hashmap 读取 csv 文件;没有得到输出
【发布时间】:2014-03-25 06:57:36
【问题描述】:

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

使用 hashmap 迭代器。代码正在读取 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,

结尾多了一个逗号。我不想那样。

请帮助解决这个问题。

在此先感谢...

【问题讨论】:

    标签: java csv iterator hashmap


    【解决方案1】:

    这里:

    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 次,因为您将令牌用作键,因此其中每个不同的令牌都有一行。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 2021-09-14
      • 1970-01-01
      相关资源
      最近更新 更多