Java 在云存储上创建仅包含10条记录的csv文件
但我只想在一个文件中写入10条记录,接下来的10条记录应该转到新的自动生成的文件名2.csv 我试过这个Java 在云存储上创建仅包含10条记录的csv文件,java,google-app-engine,google-cloud-storage,google-cloud-platform,Java,Google App Engine,Google Cloud Storage,Google Cloud Platform,但我只想在一个文件中写入10条记录,接下来的10条记录应该转到新的自动生成的文件名2.csv 我试过这个 GcsService gcsService = GcsServiceFactory.createGcsService(); String fileName=fileNameWithOutExtension+".csv"; GcsFilename gcsFileName = new GcsFilename(bucketName ,fileName);
GcsService gcsService = GcsServiceFactory.createGcsService();
String fileName=fileNameWithOutExtension+".csv";
GcsFilename gcsFileName = new GcsFilename(bucketName ,fileName);
GcsFileOptions gcsFileOptions = new GcsFileOptions.Builder().mimeType("text/csv").acl(aclType).build();
GcsOutputChannel gcsoutputChannel = gcsService.createOrReplace(gcsFileName, gcsFileOptions);
gcsoutputChannel.write(ByteBuffer.wrap(header.getBytes()));
for(T object : list){
gcsoutputChannel.write(ByteBuffer.wrap(object.toString().replace("null", "").getBytes("UTF-8")));
}
gcsoutputChannel.close();
if(StringUtility.isNotNullOrBlank(emailId)){
String downloadUrl = "https://storage.googleapis.com/"+bucketName+"/"+fileName;
EmailUtil.sendJobReportCompletionMail(emailId, downloadUrl);
}
this is my code to create CSV file on cloud storage.
但我现在很困惑。
我可以创建新文件,但如何使用相同的for循环在该文件上写入?相当多的错误和不完整的代码。对于主循环,请尝试以下操作:
GcsService gcsService = GcsServiceFactory.createGcsService();
String fileName=fileNameWithOutExtension+".csv";
int i=1,counter = 0;
GcsFilename gcsFileName = new GcsFilename(bucketName ,fileName);
GcsFileOptions gcsFileOptions = new GcsFileOptions.Builder().mimeType("text/csv").acl(aclType).build();
GcsOutputChannel gcsoutputChannel = gcsService.createOrReplace(gcsFileName, gcsFileOptions);
gcsoutputChannel.write(ByteBuffer.wrap(header.getBytes()));
for(T object : list){
if(counter == 10)
{
fileName=fileNameWithOutExtension+i+".csv";
gcsFileName = new GcsFilename(bucketName ,fileName);
gcsFileOptions = new GcsFileOptions.Builder().mimeType("text/csv").acl(aclType).build();
gcsoutputChannel = gcsService.createOrReplace(gcsFileName, gcsFileOptions);
}else
{
gcsoutputChannel.write(ByteBuffer.wrap(object.toString().replace("null", "").getBytes("UTF-8")));
}
}
gcsoutputChannel.close();
if(StringUtility.isNotNullOrBlank(emailId)){
String downloadUrl = "https://storage.googleapis.com/"+bucketName+"/"+fileName;
EmailUtil.sendJobReportCompletionMail(emailId, downloadUrl);
}
}catch(IOException e){
logger.log(Level.SEVERE,"Error wrinting to GCS:",e);
}
logger.log(Level.INFO, list.toString());
logger.log(Level.INFO,"Wrinting to GCS completed");
}
我认为你的代码有一些错误。首先,它从不修改“i”或“counter”。其次,计数器==10只会将第10条(或第11条,具体取决于您何时递增)记录准确地输出到filename2.csv
for(T object : list){
gcsoutputChannel.write(ByteBuffer.wrap(object.toString().replace("null", "").getBytes("UTF-8")));
counter += 1;
if(counter == 10)
{
gcsoutputChannel.close();
fileName=fileNameWithOutExtension+i+".csv";
i += 1;
gcsFileName = new GcsFilename(bucketName ,fileName);
gcsFileOptions = new GcsFileOptions.Builder().mimeType("text/csv").acl(aclType).build();
gcsoutputChannel = gcsService.createOrReplace(gcsFileName, gcsFileOptions);
}
}
if(counter > 0)
{
gcsoutputChannel.close();
}