java 从 byte[] 写入 csv 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37540414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:36:23  来源:igfitidea点击:

Write csv file from byte[]

javabytearrayopencsv

提问by pro_newbie

I am getting a file in byte[]which have comma separated values in quotes, I want to save it as CSV by using OpenCSV. I am using thisto save it in CSV format.

我得到一个文件,byte[]其中引号中包含逗号分隔值,我想使用 OpenCSV 将其保存为 CSV。我正在使用它以 CSV 格式保存它。

Following is my code to convert byte[] to array and then store it in file

以下是我将 byte[] 转换为数组然后将其存储在文件中的代码

byte[] bytes = myByteStream.getFile();

String decoded = new String(bytes, "UTF-8");            
//String lines[] = decoded.split("\r?\n");


FileOutputStream fos = new FileOutputStream("/home/myPC/Desktop/test.csv"); 
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
CSVWriter writer = new CSVWriter(osw);
String[] row = {decoded};

writer.writeNext(row);
writer.close();
osw.close();

But this above code puts extra quotes around and also merge all lines in one line.

但是上面的代码添加了额外的引号并将所有行合并为一行。

Any help on how to do this properly ?

有关如何正确执行此操作的任何帮助?

回答by uniknow

You can prevent adding quotes to the cell values within the constructor of CSVWriter, for example:

您可以防止在 的构造函数中CSVWriter为单元格值添加引号,例如:

CSVWriter writer = new CSVWriter(osw, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);

Regarding the whole byte array persisted as a single row. Are you sure there are newlines within the original file.

关于整个字节数组作为单行持久化。您确定原始文件中有换行符吗?

If so you might get away by doing the following:

如果是这样,您可能会通过执行以下操作来逃脱:

    BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "ASCII"));

String line;
while ((line = reader.readLine()) != null) {
    // Handle the line, ideally in a separate method
}

Got this from splitting a byte array on a particular byte

通过在特定字节上拆分字节数组得到这个

回答by Raffaele

I think Apache Commons CSVis a better CSV library. From the pasted code it is not clear if you want to somehow change the contents of the file or just duplicate it: in the latter case you don't even need to parse the file as CSV records - just copy it byte-for-byte. In the former case, ie if you need to modify the content of the file, you need something like (Commons CSV used here)

我认为Apache Commons CSV是一个更好的 CSV 库。从粘贴的代码中,不清楚您是想以某种方式更改文件的内容还是只是复制它:在后一种情况下,您甚至不需要将文件解析为 CSV 记录 - 只需逐字节复制它. 在前一种情况下,即如果您需要修改文件的内容,则需要类似(此处使用Commons CSV)

CSVParser parser = CSVFormat.newFormat(',').parse(
    new InputStreamReader(new ByteArrayInputStream(bytes), "UTF8"));
CSVPrinter printer = CSVFormat.newFormat(',').print(out);
for (CSVRecord record : parser) {
  try {
    printer.printRecord(record);
  } catch (Exception e) {
    throw new RuntimeException("Error at line "
      + parser.getCurrentLineNumber(), e);
  }
}
parser.close();
printer.close();

Look at the Javadoc for CSVFormat

查看 Javadoc CSVFormat