1

在标准 maven 项目的资源目录中有一个 csv 文件,如下所示:

src/main/resources/fruits.csv
src/test/resources/fruits.csv

水果.csv

Type, Quantity
apple, 50
banana, 60
orange, 70

使用以下库

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.0</version>
</dependency>

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

水果(标准 POJO)

public class Fruit {

    private String type
    private int quantity;

    public Fruit(String type, int quantity) {
        this.type = type;
        this.quantity = quantity;
    }

    // Getters & Setters
}

CsvFileReader

public class CsvFileReader {

    private static final String [] FILE_HEADER = {""};

    private static final String TYPE = "Type";
    private static final String QUANTITY + "Quantity";

    public static List<Candidate> readCsvFile(File fileName) {
        FileReader fileReader = null;
        CSVParser csvFileParser = null;

        CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER);
        List<Candidate> fruits = null;
        try {
            fruits = new ArrayList<>();
            String file = FileUtils.readFileToString(fileName);
            fileReader = new FileReader(file);
            csvFileParser = new CSVParser(fileReader, csvFormat);
            List<CSVRecord> records = csvFileParser.getRecords();

            for (int i = 1; i < records.size(); i++) {
                CSVRecord record = records.get(i);
                Fruit fruit = new Fruit(record.get(TYPE), Integer.parseInt(QUANTITY));
                fruits.add(fruit);
            }
        } 
        catch(Throwable t) {
            t.printStackTrace();
        } 
        finally {
            try {
                fileReader.close();
                csvFileParser.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
        return fruits;
    }
}

CsvFileReaderTest(JUnit 4 测试用例):

public class CsvFileReaderTest {

    File csvFile = null;

    @Before
    public void setUp() throws Exception {
        String userDir = System.getProperty("user.dir");
        String filePath = userDir + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator;
        csvFile = FileUtils.getFile(filePath + "fruits.csv");
    }
}

当我在 Eclipse 中运行 JUnit 测试用例时:

java.io.FileNotFoundException: Type, Quantity
apple, 50
banana, 60
orange, 70
(File name too long)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:101)
    at java.io.FileReader.<init>(FileReader.java:58)

似乎在读取文件,但问题是文件名太长了?

我究竟做错了什么?

4

1 回答 1

0

FileReader构造函数需要一个传递文件内容的文件路径。您可以直接使用CSVParser采用 CSV 的构造函数String

csvFileParser = new CSVParser(file /*This is already file content*/, csvFormat);

** 编辑 **

更好的是获取资源的阅读器并将其传递给 CSVParser 构造函数:

reader = new BufferedReader(new InputStreamReader(
                       this.getClass().getResourceAsStream("fruits.csv")));
csvFileParser = new CSVParser(reader, csvFormat);
于 2015-11-12T08:05:01.000 回答