【发布时间】:2016-01-28 17:31:06
【问题描述】:
我正在编写一个我还需要压缩的 csv 文件,并且正在使用 java.util.zip.ZipEntry 和 java.util.zip.ZipOutputStream。
当我在所有列中都有西方字符时,一切都很好,但是当我使用韩语字符时,它无法识别 /n 并且所有内容都出现在同一行上。我将其写为 UTF-8 字符,并希望这涵盖韩语。
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class CreateCSV {
public static void main(String[] args) throws IOException {
DateTime utcDateTime = new DateTime().toDateTime(DateTimeZone.UTC);
DateTime newDateTime = utcDateTime.toDateTime();
DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("yyyyMMdd-HHmmss-SSS-");
File zipFile = new File("C:/TestCSVKorean/"+ dateFormatter.print(newDateTime) + "Export.zip");
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
// Open up the zipfile and create the csv entry
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fileOutputStream));
zos.putNextEntry(new ZipEntry(dateFormatter.print(newDateTime) + "tics.csv"));
// The first line of the CSV is a header line
StringBuffer csvHeader = new StringBuffer(
"Time,Name,Rev,Appme,EnvName,PlanName,PlanRev,"
+ "Og Name,Op Name,"
+ "TTR,Lat,Bys Rec,Bytt,"
+ "RPl,Request Method,URI Path,Query String,HTTP Status Code,"
+ "HTTP Request Headers,User Agent,Request Body,HTTP Response Headers,Response Body\n");
zos.write(csvHeader.toString().getBytes(), 0, csvHeader.length());
StringBuffer csvData = new StringBuffer("");
csvData.append("\"" + newDateTime + "\",\"" +
"apiName" + "\"," +
"2.0.0" + ",\"" +
"app name" + "\",\"" +
"env name" + "\",\"" +
"plan name" + "\"," +
"2" + ",\"" +
"dev org name" + "\",\"" +
"ìº˜ë¦°ë” ëª©ë¡ì¡°íšŒ(ë‚´ 캘린ë”, 구ë…가능한 캘린ë”, 시스템 캘린ë”, ê´€ë¦¬ìž ìº˜ë¦°ë”)" + "\"," +
"123" + "," +
"inifd: 334;dshs: 343" + ", " +
"10" + "," +
"33" + ",\"" +
"http" + "\",\"" +
"GET" + "\",\"" +
"/dsfs/sdf/ds" + "\",\"" + "query string" + "\",\"" +
"200" + "\",\"" +
"jshkshdf" + "\",\"" +
"sdjhfks/sdfs/" + "\",\"" +
"jhksdfhks dsfs" + "\",\"" +
"dsfsdfs" + "\",\"" +
"dsfsfs" + "\"\n");
zos.write(csvData.toString().getBytes("UTF-8"), 0, csvData.length());
csvData = new StringBuffer("");
csvData.append("\"" + newDateTime + "\",\"" +
"apiName" + "\"," +
"2.0.0" + ",\"" +
"app name" + "\",\"" +
"env name" + "\",\"" +
"plan name" + "\"," +
"2" + ",\"" +
"dev org name" + "\",\"" +
"ìº˜ë¦°ë” ëª©ë¡ì¡°íšŒ(ë‚´ 캘린ë”, 구ë…가능한 캘린ë”, 시스템 캘린ë”, ê´€ë¦¬ìž ìº˜ë¦°ë”)" + "\"," +
"123" + "," +
"inifd: 334;dshs: 343" + ", " +
"10" + "," +
"33" + ",\"" +
"http" + "\",\"" +
"GET" + "\",\"" +
"/dsfs/sdf/ds" + "\",\"" + "query string" + "\",\"" +
"200" + "\",\"" +
"jshkshdf" + "\",\"" +
"sdjhfks/sdfs/" + "\",\"" +
"jhksdfhks dsfs" + "\",\"" +
"dsfsdfs" + "\",\"" +
"dsfsfs" + "\"\n");
zos.write(csvData.toString().getBytes("UTF-8"), 0, csvData.length());
zos.close();
}
}
这是我打开 csv 文件时看到的内容:
时间名称 Rev Appme EnvName PlanName PlanRev Og Name Op Name TTR Lat Bys Rec Bytt RPl Request Method URI Path Query String HTTP Status Code HTTP Request Headers User Agent Request Body HTTP Response Headers Response Body
2016-01-28T17:20:56.859Z apiName 2.0.0 应用程序名称环境名称计划名称 2 开发组织名称ÃÅ¡Å'(ë‚´ 캘린ëÂâ€, 구뀦가능Õœ 캘ë °ë€, 시스Ã…Ŕ 캘린ë€, ê´€ë¦Â¬Ã ¬Å¾Â 캘린ë€) 123 inifd: 334;dshs: 343 10 33 http 2016-01-28T17:20:56.859Z apiName 2.0.0 应用程序名称环境名称计划名称 2 开发组织名称캘린ë†목ë¡Âì¡°ÃÅ¡Å'(ë‚´ 캘린ëÂâ€, 구뀦가능՜캘린ë€, 시ìŠ¤Ã…Š“ 캘린ë€, ê´€ë¬ìž ìº˜ë¦°ëÂâ€) 123 inifd: 334;dshs: 343 10 33 http
它将第二行的日期字段粘贴到第一行的请求方法字段中:2016-01-28T17:20:56.859Z
【问题讨论】:
-
您的 CSV 文件看起来正确。问题在于你如何看待它。你用什么程序打开它?