简体   繁体   English

Java读取CSV文件,内容不写入新的CSV文件

[英]Java read CSV file ,contents not write in new CSV file

package com;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import com.opencsv.CSVWriter;
import com.opencsv.CSVReader;

public class Sample2 {
    public static void main(String args[]) throws IOException
    {
        CSVReader csvReader = null;  
        String[] employeeDetails ;        
        CSVWriter  csvWriter = new CSVWriter(new FileWriter("D:\\sample\\myfile.csv",true));
        csvReader = new CSVReader(new FileReader("D:\\sample\\source.csv"));          
        try
        {        

            employeeDetails = csvReader.readNext();               
            while ((employeeDetails = csvReader.readNext()) != null ) {               

                    System.out.println(Arrays.toString(employeeDetails));                   
                    csvWriter.writeNext(employeeDetails);
                }               

        }catch(Exception ee)
            {
                ee.printStackTrace();
            }
        }
}

I have my above java code It read data from source.csv file and also display in the console .我有我上面的 java 代码它从 source.csv 文件中读取数据并显示在控制台中。 It created myfile.csv ,but same contents it didn't write in the csv file Anyone have any idea on this它创建了 myfile.csv ,但它没有在 csv 文件中写入相同的内容任何人都对此有任何想法

CSVWriter implements Flushable.Working Solution is already present in @Stephan Hogenboom's answer. CSVWriter 实现 Flushable.Working 解决方案已经存在于@Stephan Hogenboom 的回答中。 I will answer why didn't it write in your case,我会回答为什么没有写在你的情况下,

From the javadocs of Flushable interface,Flushable接口的 javadocs,

A Flushable is a destination of data that can be flushed. Flushable 是可以刷新的数据的目的地。 The flush method is invoked to write any buffered output to the underlying stream.调用flush 方法将任何缓冲输出写入底层流。

For performance reasons, all data is to be written into a Buffer instead of File temporarily.出于性能原因,所有数据都将临时写入 Buffer 而不是 File。 Once you call the flush() method, it flushes the data already present in the buffer into your file(this is where disk I/O happens, not when you call writeNext() ).一旦您调用了 flush() 方法,它就会将缓冲区中已经存在的数据刷新到您的文件中(这是磁盘 I/O 发生的地方,而不是您调用writeNext() )。

As mentioned on doc of flush() in java.io.Writer .java.io.Writerflush()文档所述。

Flushes the stream.冲洗流。 If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination.如果流已将来自各种 write() 方法的任何字符保存在缓冲区中,则立即将它们写入其预期目的地。

The issue is that you don't close your output resources, try this code:问题是您没有关闭输出资源,请尝试以下代码:

public static void main(String args[]) throws IOException {
String[] employeeDetails;

try (CSVWriter csvWriter = new CSVWriter(new FileWriter("D:\\sample\\myfile.csv", true));
    CSVReader csvReader = new CSVReader(new FileReader("D:\\sample\\source.csv"));
    ) {

  while ((employeeDetails = csvReader.readNext()) != null) {

    System.out.println(Arrays.toString(employeeDetails));
    csvWriter.writeNext(employeeDetails);
  }
}
catch (Exception ee) {
  ee.printStackTrace(); //perhaps you should also log the error?
}
}

Also take a look at this question Is closing the resources always important?也看看这个问题关闭资源总是重要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM