Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java CSVPrinter未打印到Zip_Java_Io_Export To Csv_Zipfile_Bufferedwriter - Fatal编程技术网

Java CSVPrinter未打印到Zip

Java CSVPrinter未打印到Zip,java,io,export-to-csv,zipfile,bufferedwriter,Java,Io,Export To Csv,Zipfile,Bufferedwriter,我有一种方法,通过将图像和注释以csv格式打包在一起来保存所有数据。我使用CSVPrinter打印到CSV,我试图将其附加到zip文件,但CSV文件最终为空。我已经检查了我传递的记录是否可以访问并且有值 publicstaticvoidmycolmethod(arraylistimgs,stringpath)抛出IOException{ 文件zipFile=新文件(路径); 布尔结果=zipFile.createNewFile(); 如果(!结果) 返回; 字符串[]点头=新字符串[]{“x”、

我有一种方法,通过将图像和注释以csv格式打包在一起来保存所有数据。我使用CSVPrinter打印到CSV,我试图将其附加到zip文件,但CSV文件最终为空。我已经检查了我传递的记录是否可以访问并且有值

publicstaticvoidmycolmethod(arraylistimgs,stringpath)抛出IOException{
文件zipFile=新文件(路径);
布尔结果=zipFile.createNewFile();
如果(!结果)
返回;
字符串[]点头=新字符串[]{“x”、“y”、“类”};
如果(!结果)
返回;
BufferedOutputStream bos=新的BufferedOutputStream(新文件输出流(zipFile));
try(ZipOutputStream out=新的zipoutpstream(bos)){
对于(ImageDataObject img:imgs){
字符串imgName=img.getImageName();
文件f=新文件(img.getImgPath());
如果(!f.exists())
继续;
out.putNextEntry(新ZipEntry(imgName));
复制(f.toPath(),out);
out.closeEntry();
out.putNextEntry(新ZipEntry(imgName+“.csv”);
CSVPrinter writer=新的CSVPrinter(新的OutputStreamWriter(out),CSVFormat.DEFAULT.withHeader(pointheader));
对于(PcdPoint PcdPoint:img.getPointList()){
printRecord(pcdPoint.x,pcdPoint.y,pcdPoint.getType());
}
out.closeEntry();
}
}捕获(IOE异常){
投掷e;
}
bos.close();
}

有关更多信息,请参见-pcdPoint扩展点,x和y是类的公共int成员,getType()也返回一个整数,即所有原语。

上述代码中的
CSVPrinter
流使用
OutputStreamWriter
,因此它会累积写入的字节,直到关闭为止。您需要关闭
CSVPrinter
,以便写入内容

或者,您可以使用
PrintStream
而不是
OutputStreamWriter
,因为它会立即将内容打印到CSV文件中,但不会抛出
IOException
,因此使用其方法
checkError()
检查错误非常重要


否则,您可以使用任何
OutputStream
来包装实现
appendeable
接口的
zipoutpstream
,甚至您自己的接口。

您忘记关闭
writer
。事实上,不要调用close(),只需将每个closeable放在try with resources块中即可!。。。但是,通过再次检查文档,我发现了一个更简单的解决方案,我将发布答案