如何实现csv文件的读写操作
请教各位大侠,谁有写过实现对csv文件的读写操作的java代码,有源码的话帮忙贴上来,我是一点思路都没有~~~~
------解决方案--------------------简单啊
------解决方案--------------------朋友,你是要对以csv为后缀的文件进行读写操作吧,如果是,那我可以给你个网址你去参考吧!
http://www.1cn.biz/java/index.php?q=node/237
------解决方案--------------------CVS 写操作部分代码。
ZZDownloadUtil.createCsv(
outList,
"KT_CEIT.dat ",
csvHeader,
response);
================================================
public static void createCsv(
final List csvList,
final String csvFileName,
final String csvHeader,
final HttpServletResponse response)
throws ZZ
ServerException {
OutputStream outputStream = null;
try {
ZZDownloadUtil.setResponseBeforeDownload(response, csvFileName);
outputStream = response.getOutputStream();
ZZCsvWriter.writeCSV(csvList, csvHeader, null, outputStream);
} catch (
IOException e) {
} catch (Exception e) {
} finally {
if (null != outputStream) {
try {
outputStream.close();
} catch (Exception e) {
throw new ZZServerException(e.getMessage());
}
}
}
}
public static final void setResponseBeforeDownload(
HttpServletResponse response,
String filename)
throws ZZServerException {
try {
response.setCharacterEncoding( "Shift_JIS ");
response.setContentType( "application/x-msbinary ");
response.setHeader(
"Content-Disposition ",
"attachment; filename= "
+ new String(filename.getBytes( "Shift_JIS "), "ISO8859_1 "));
} catch (Unsupported
EncodingException e) {
}
}
public static void writeCSV(
List data,
String headerStr,
String tailStr,
OutputStream outputStream) throws IOException {
String csvString = convertDataToCSV(data, headerStr, tailStr);
Writer sw = null;
try {
if (outputStream != null) {
String charset = "Windows-31J ";
sw = new BufferedWriter(
new OutputStreamWriter(outputStream, charset));
//sw = new BufferedWriter(new OutputStreamWriter(outputStream));
sw.write(csvString);
}
} finally {
if (sw != null) {
sw.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
public static String convertDataToCSV(
List data,
String headerString,
String tailString) {
StringBuffer csvData = new StringBuffer();
if (!ZZStringUtil.isNull(headerString)) {
csvData.append(headerString);
csvData.append(STR_CRLF);
}
convertDataToString(data, csvData);
csvData.append(STR_CRLF);
if (!ZZStringUtil.isNull(tailString)) {
csvData.append(tailString);