Java-使用Apache.commons.CSV编写CSV文件
我在用电话。我正在使用以下代码从网页读取CSV文件:Java-使用Apache.commons.CSV编写CSV文件,java,csv,apache-commons-csv,Java,Csv,Apache Commons Csv,我在用电话。我正在使用以下代码从网页读取CSV文件: InputStream input = new URL(url).openStream(); Reader reader = new InputStreamReader(input, "UTF-8"); defaultParser = new CSVParser(reader, CSVFormat.DEFAULT); excelParser = new CSVParser(reader, CS
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();
但是,我在这个库中找不到一种方法可以轻松地将这个文件写入我的计算机,以便打开它并在以后从中读取
我尝试了这个代码来保存文件
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();
但是,当我尝试使用此代码读取文件时,没有输出任何内容:
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));
它给出了一个
线程“main”java.lang.ArrayIndexOutOfBoundsException中出现异常异常:1
当我用记事本打开保存的CSV文件时,有一个空行,然后:
2016-03-04714.98999716.48999706.02002710.8900151967900710.890015,“
“,2016-03-03718.679993719.450012706.02002712.4199831956800712.419983,”,2016-03-02719.00720.00712.00718.8499761627800718.849976,”
看起来您正在同一行打印所有记录 其他方法(如)将更有帮助:
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();
您是否尝试过刷新并关闭CSVPrinter,而不是FileWriter?正确且良好。下面是一个变体,它更短、更现代 在这里,我们:
- 使用现代Java提供的、和类来简化文件处理工作
- 对于大量数据,请使用a以获得更好的性能
- 指定要使用的。通常是最好的。如果您不理解
- 包括与文件相关的异常所需的
- 添加自动关闭文件的语法
- 跳过显式刷新,因为作为自动关闭
和BufferedWriter
的一部分,缓冲写入程序将自动刷新。引用Javadoc,调用“关闭流,首先刷新”CSVPrinter
编辑:缺少括号。您所说的“无法正确写入数据”是什么意思“?@Berger嗯,我肯定我没有正确编写代码。它保存的文件与从网页读取的文件不同。我想在Apache Commons中应该有一个简单的内置方法来保存解析器中的文件,但我找不到,所以我尝试这样做。您能显示一个示例输入和输出吗?@Berger当我尝试在Excel中打开它时,我收到一个错误“文件未完全加载”。我用从保存的文件中读取的代码更新了OP。使用简单的文本编辑器(记事本,无论什么…)打开它,并将内容粘贴到您的问题中。我没有关闭CSVPrinter thank you@ionFreeman。
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();
CSVFormat format = CSVFormat.EXCEL.withHeader();
Path path = Paths.get( savePath + ".csv" );
try (
BufferedWriter writer = Files.newBufferedWriter( path , StandardCharsets.UTF_8 ) ;
CSVPrinter printer = new CSVPrinter( writer , format ) ;
)
{
printer.printRecords( excelParser.getRecords() );
} catch ( IOException e )
{
e.printStackTrace();
}