Java-使用Apache.commons.csv写入CSV文件 [英] Java - Write CSV File with Apache.commons.csv

查看:1406
本文介绍了Java-使用Apache.commons.csv写入CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java中的 apache.commons.csv库.我正在使用以下代码从网页中读取CSV文件:

I'm using the apache.commons.csv library in Java. I'm reading a CSV file from a web page with this code:

InputStream input = new URL(url).openStream();
        Reader reader = new InputStreamReader(input, "UTF-8");

        defaultParser = new CSVParser(reader, CSVFormat.DEFAULT);
        excelParser = new CSVParser(reader, CSVFormat.EXCEL.withHeader()); 

        defaultParsedData = defaultParser.getRecords();
        excelParsedData = excelParser.getRecords();

但是,我无法在该库中找到一种方法来轻松地将此文件写入计算机,以便稍后打开并读取.

However, I can't find a method in this library to easily write this file to my computer in order to open it up and read from it later on.

我尝试使用此代码保存文件.

I tried this code to save the file.

String outputFile = savePath+".csv";
        CSVPrinter csvFilePrinter = null;
        CSVFormat csvFileFormat = CSVFormat.EXCEL.withHeader();
        FileWriter fileWriter = new FileWriter(outputFile);
        csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);

        for (CSVRecord csvRecord : excelParser) {
            for(String dataPoint: csvRecord){
                csvFilePrinter.print(dataPoint);
            }
            csvFilePrinter.print('\n');
         }

        fileWriter.flush();
        fileWriter.close();
        csvFilePrinter.close();

但是,当我尝试使用此代码读取文件时,没有任何输出:

However, when I try to read the file with this code, nothing prints out:

InputStream input = new FileInputStream(cvsFilePath);
        Reader reader = new InputStreamReader(input, "UTF-8");

        CSVParser load = new CSVParser(reader, CSVFormat.EXCEL);
        //TEST THAT IT WORKED
        java.util.List<CSVRecord> testlist = load.getRecords();
        CSVRecord dataPoint = testlist.get(0);
        System.out.println("print: " + dataPoint.get(0));

这仅打印出"print:" 如果我添加

This only prints out "print: " If I add

System.out.println("print: " + dataPoint.get(1));

它给出一个

线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:1

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

当我用记事本打开保存的CSV文件时,出现空白行,然后:

When I open the saved CSV file with notepad there is a blank line and then:

2016-03-04,714.98999,716.48999,706.02002,710.890015,1967900,710.890015, ,2016-03-03,718.679993,719.450012,706.02002,712.419983,1956800,712.419983,"",2016-03-02,719.00,720.00,712.00,718.849976,1627800,718.849976,"

2016-03-04,714.98999,716.48999,706.02002,710.890015,1967900,710.890015," ",2016-03-03,718.679993,719.450012,706.02002,712.419983,1956800,712.419983," ",2016-03-02,719.00,720.00,712.00,718.849976,1627800,718.849976,"

推荐答案

似乎您将所有记录打印在同一行上.

It looks like you are printing all the records on the same line .

其他方法,例如 printRecords 会更有用:

Other methods like printRecords will be more helpful :

String outputFile = savePath+".csv";
CSVPrinter csvFilePrinter = null;
CSVFormat csvFileFormat = CSVFormat.EXCEL.withHeader();
FileWriter fileWriter = new FileWriter(outputFile);
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);

csvFilePrinter.printRecords(excelParser.getRecords());


fileWriter.flush();
fileWriter.close();
csvFilePrinter.close();

这篇关于Java-使用Apache.commons.csv写入CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆