如何实现csv文件的读写操作 请教各位大侠,谁有写过实现对csv文件的读写操作的java代码,有源码的话帮忙贴上来,我是一点思路都没有~~~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 朋友,你是要对以csv为后缀的文件进行读写操作吧,如果是,那我可以给你个网址你去参考吧!http://www.1cn.biz/java/index.php?q=node/237 是要对以csv为后缀的文件进行读写操作,谢谢楼上的,我去那网站看看~~ 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 ZZServerException { 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 (UnsupportedEncodingException 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); csvData.append(STR_CRLF); } return csvData.toString(); } private static void convertDataToString(List data, StringBuffer csvData) { if (data == null) { return; } final int rowSize = data.size(); for (int iRow = 0; iRow < rowSize; iRow++) { List rowData = (List) data.get(iRow); final int colSize = rowData.size(); for (int iCol = 0; iCol < colSize; iCol++) { csvData.append(adjustCsv((String) rowData.get(iCol))); if (iCol < colSize - 1) { csvData.append(STR_COMMA); } } if (iRow < rowSize - 1) { csvData.append(STR_CRLF); } } } public static String adjustCsv(String paramObj) { if (paramObj == null) { paramObj = ""; } String str = paramObj; if (0 <= str.indexOf(STR_CRLF)) { str = str.replaceAll(STR_CRLF, STR_LF); } if (0 <= str.indexOf(STR_CR)) { str = str.replaceAll(STR_CR, STR_LF); } boolean isNecessary = false; if (0 <= str.indexOf(STR_LF)) { isNecessary = true; } if (0 <= str.indexOf(STR_COMMA)) { isNecessary = true; } if (0 <= str.indexOf(STR_DOUBLE_QUART)) { str = str.replaceAll(STR_DOUBLE_QUART, STR_DOUBLE_QUART + STR_DOUBLE_QUART); isNecessary = true; } if (isNecessary) { StringBuffer sb = new StringBuffer(); sb.append(STR_DOUBLE_QUART); sb.append(str); sb.append(STR_DOUBLE_QUART); str = sb.toString(); } return str; } public static final String STR_COMMA = ","; public static final String STR_DOUBLE_QUART = "\""; public static final String STR_CRLF = "\r\n"; public static final String STR_LF = "\n"; public static final String STR_CR = "\r"; 读的话,就纯粹是对FormFile的操作了。 java网络编程——C/S聊天室 GridLayout布局的问题 很简单的问题 serverSocket报错,代码如下,求助!!! 线程问题! 问个"\"符号的问题 integer.parseInt的疑惑 十分简单的问题,但对我却十分头疼,请各位高手指教,在线等待,高分 我是新手,求救! 一个有关内部类的问题(送分) tomcat如何实现双机热备? 关于一道有关'多态'的面试题
http://www.1cn.biz/java/index.php?q=node/237
outList,
"KT_CEIT.dat",
csvHeader,
response);================================================
public static void createCsv(
final List csvList,
final String csvFileName,
final String csvHeader,
final HttpServletResponse response)
throws ZZServerException { 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 (UnsupportedEncodingException 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);
csvData.append(STR_CRLF);
} return csvData.toString(); } private static void convertDataToString(List data, StringBuffer csvData) {
if (data == null) {
return;
} final int rowSize = data.size();
for (int iRow = 0; iRow < rowSize; iRow++) {
List rowData = (List) data.get(iRow);
final int colSize = rowData.size();
for (int iCol = 0; iCol < colSize; iCol++) { csvData.append(adjustCsv((String) rowData.get(iCol)));
if (iCol < colSize - 1) {
csvData.append(STR_COMMA);
}
} if (iRow < rowSize - 1) {
csvData.append(STR_CRLF);
}
}
}
public static String adjustCsv(String paramObj) {
if (paramObj == null) {
paramObj = "";
}
String str = paramObj; if (0 <= str.indexOf(STR_CRLF)) {
str = str.replaceAll(STR_CRLF, STR_LF);
} if (0 <= str.indexOf(STR_CR)) {
str = str.replaceAll(STR_CR, STR_LF);
} boolean isNecessary = false; if (0 <= str.indexOf(STR_LF)) {
isNecessary = true;
} if (0 <= str.indexOf(STR_COMMA)) {
isNecessary = true;
} if (0 <= str.indexOf(STR_DOUBLE_QUART)) {
str = str.replaceAll(STR_DOUBLE_QUART,
STR_DOUBLE_QUART + STR_DOUBLE_QUART);
isNecessary = true;
} if (isNecessary) {
StringBuffer sb = new StringBuffer();
sb.append(STR_DOUBLE_QUART);
sb.append(str);
sb.append(STR_DOUBLE_QUART);
str = sb.toString();
}
return str;
}
public static final String STR_COMMA = ","; public static final String STR_DOUBLE_QUART = "\""; public static final String STR_CRLF = "\r\n"; public static final String STR_LF = "\n"; public static final String STR_CR = "\r";