package com.hoperun.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.bytecode.opencsv.CSVWriter;
public class CsvUtil1 {
private String filename = null;
private BufferedReader bufferedreader = null;
private File file;
private List<String[]> list = new ArrayList<String[]>();
public CsvUtil1() {
}
public CsvUtil1(String filename) throws IOException {
this.filename = filename;
file = new File(filename);
bufferedreader = new BufferedReader(new FileReader(filename));
String stemp;
while ((stemp = bufferedreader.readLine()) != null) {
String[] objs = stemp.split(",");
list.add(objs);
}
bufferedreader.close();
}
public void setList(List list) {
this.list = list;
}
public CsvUtil1(File file) throws IOException {
this.file = file;
this.filename = file.getName();
bufferedreader = new BufferedReader(new FileReader(file));
String stemp;
while ((stemp = bufferedreader.readLine()) != null) {
String[] objs = stemp.split(",");
list.add(objs);
}
bufferedreader.close();
}
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 static String removestr(String str) {
StringBuffer sb = new StringBuffer(str);
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == 34) {
sb.deleteCharAt(i);
}
}
return sb.toString();
}
public void removeStrInList() {
for (String[] strs : list) {
for (int i = 0; i < strs.length; i++) {
strs[i] = CsvUtil1.removestr(strs[i]);
}
}
}
public void writefile() throws Exception {
for (String[] strs : list) {
for (int i = 0; i < strs.length; i++) {
strs[i] = CsvUtil1.removestr(strs[i]);
}
}
Writer writer = new FileWriter(file);
CSVWriter csvwriter = new CSVWriter(writer);
csvwriter.writeAll(list);
csvwriter.close();
writer.close();
}
public void addRecord(String[] strs) throws Exception {
String[] strArray = { String.valueOf(list.size()), strs[0], strs[1],
strs[2], strs[3], strs[4] };
list.add(strArray);
writefile();
}
public void updaterecord(String[] strs) throws Exception {
String[] srcStrs = list.get(Integer.parseInt(strs[0]));
for (int i = 1; i < strs.length; i++) {
srcStrs[i] = strs[i];
}
writefile();
}
}