您的位置:首页 > 编程语言 > Java开发

java读取CSV文件

2009-12-13 14:11 316 查看
public class CsvUtil1 {
private String filename = null;

private BufferedReader bufferedreader = null;

private List list = new ArrayList();

public CsvUtil1() {

}

public CsvUtil1(String filename) throws IOException {
this.filename = filename;
bufferedreader = new BufferedReader(new FileReader(filename));
String stemp;
while ((stemp = bufferedreader.readLine()) != null) {
list.add(stemp);
}
}

public List getList() throws IOException {
return list;
}

public int getRowNum() {
return list.size();
}

public int getColNum() {
if (!list.toString().equals("[]")) {
if (list.get(0).toString().contains(",")) {
return list.get(0).toString().split(",").length;
} else if (list.get(0).toString().trim().length() != 0) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
}

public String getRow(int index) {
if (this.list.size() != 0)
return (String) list.get(index);
else
return null;
}

public String getCol(int index) {
if (this.getColNum() == 0) {
return null;
}
StringBuffer scol = new StringBuffer();
String temp = null;
int colnum = this.getColNum();
if (colnum > 1) {
for (Iterator it = list.iterator(); it.hasNext();) {
temp = it.next().toString();

scol = scol.append(temp.split(",")[index] + ",");
}
} else {
for (Iterator it = list.iterator(); it.hasNext();) {
temp = it.next().toString();
scol = scol.append(temp + ",");
}
}
String str = new String(scol.toString());
str = str.substring(0, str.length() - 1);
return str;
}

public String getString(int row, int col) {
String temp = null;
int colnum = this.getColNum();
if (colnum > 1) {
temp = list.get(row).toString().split(",")[col];
} else if (colnum == 1) {
temp = list.get(row).toString();
} else {
temp = null;
}
return temp;
}

public void CsvClose() throws IOException {
this.bufferedreader.close();
}

public void test() throws IOException {
CsvUtil1 cu = new CsvUtil1("D:/学习/00dw.csv");
List tt = cu.getList();
for (Iterator itt = tt.iterator(); itt.hasNext();) {
System.out.println(itt.next().toString()+"||");
}
// System.out.println(cu.getRowNum());
// System.out.println(cu.getColNum());
// System.out.println(cu.getRow(0));
// System.out.println(cu.getCol(0));
// System.out.println(cu.getString(0, 0));
cu.CsvClose();

}

public void createCsvTest1(HttpServletResponse Response) throws IOException {
CsvUtil1 cu = new CsvUtil1("D:/学习/00dw.csv");
List tt = cu.getList();
String data = "";

SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMddHHmm");
Date today = new Date();
String dateToday = dataFormat.format(today);
File file=new File("D:/学习/001dw.csv");
if(!file.exists())
file.createNewFile();
// else
// file.delete() ;
String str[] ;
StringBuilder sb = new StringBuilder("");
BufferedWriter output=new BufferedWriter(new FileWriter(file,true));
for (Iterator itt = tt.iterator(); itt.hasNext();) {
String fileStr = itt.next().toString() ;
str = fileStr.split(",");
for(int i=0;i<=str.length-1;i++){ //拆分成数组 用于插入数据库中
System.out.print("str["+i+"]="+str[i]+" ");
}
System.out.println("");
sb.append(fileStr+"/r/n") ;
}
//System.out.println(sb.toString());
output.write(sb.toString());
output.flush() ;
output.close();
cu.CsvClose();

}
public static void main(String[] args) throws IOException {
CsvUtil1 test = new CsvUtil1();
//test.test();
HttpServletResponse response = null ;
test.createCsvTest1(response);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: