其实是一个公司的笔试题,上网查查,发现网上的代码都是不完全正确的。没有考虑到一些极端的例子。贴段代码,方便自己以后查看。
主要是分析csv文件内容比较麻烦,特别是出来引号中的逗号。其他的都没什么。
package com.ltm.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class CsvHandler {
private List<List<String>> result;
CsvHandler() {
result = new ArrayList<List<String>>();
}
public void readerCSV(InputStream is) throws IOException {
String filePath = "H:\\pg\\test.csv";
BufferedReader in = new BufferedReader(new FileReader(filePath));
String str = "";
while ((str = in.readLine()) != null) {
List<String> r = new ArrayList<String>();
str = decodeString(str);
String[] s = str.split(",");
for (int i = 0; i < s.length; i++) {
String temp = s[i].replace("|", ",");
if (temp.startsWith("\"")) {
temp = temp.substring(1, temp.length() - 1);
}
temp = temp.replace("\"\"", "\"");
r.add(temp);
// System.out.println(temp);
}
result.add(r);
}
in.close();
}
/**
* 用户退出时自动将内存里的数据写入服务器文件中或者直接提示用户下载
*
* @param savePath
* @throws IOException
*/
public void closeCSV() throws IOException {
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMddHHmm");
Date today = new Date();
String filename = dataFormat.format(today);
File file = new File(filename+".csv");
if (!file.exists()){
file.createNewFile();
}
BufferedWriter out = new BufferedWriter(new FileWriter(file));
String temp = new String();
for (List<String> s : result) {
for (String str : s) {
temp += encodeString(str) + ",";
}
temp = temp.substring(0, temp.length() - 1);
out.write(temp);
temp = "";
out.newLine();
}
out.flush();
out.close();
}
public void editCsv(int row, int col, String s) {
this.result.get(row).set(col, s);
}
public void addDate(String[] s) {
List<String> e = new ArrayList<String>();
for (String st : s) {
e.add(st);
}
this.result.add(e);
}
public static String encodeString(String str) {
if (str.indexOf("\"") != -1 || str.indexOf(",") != -1) {
str = "\"" + str.replace("\"", "\"\"") + "\"";
}
return str;
}
/**
* 将""中的,转变为|
*
* @param args
*/
public static String decodeString(String str) {
int a = 0;
int b = 0;
int c = 0;
a = str.indexOf("\"");
while (a != -1) {
c = str.indexOf(",", a);
b = str.indexOf("\"", a + 1);
if (c == -1)
break;
if (a < c && c < b) {
String temp = str.substring(a + 1, b);
temp = temp.replace(",", "|");
str = str.substring(0, a + 1) + temp
+ str.substring(b, str.length());
}
a = str.indexOf("\"", b + 1);
}
return str;
}
public static void main(String[] args) {
CsvHandler cs = new CsvHandler();
try {
String[] a = { "sfds,df", "\"hi\"", "hibaby" };
cs.readerCSV(null);
cs.addDate(a);
cs.closeCSV();
} catch (IOException e) {
e.printStackTrace();
}
}
// 获取列数
public int getCol() {
int temp = 0;
for (int i = 0; i < this.result.size(); i++) {
if (temp < this.result.get(i).size()) {
temp = this.result.get(i).size();
}
}
return temp;
}
// 获取最大的行数
public int getRow() {
return this.result.size();
}
}