Csv工具类
import com.csvreader.CsvReader;
import com.soft.backstage.alipay.bean.AlipayBill;
import com.soft.backstage.alipay.bean.AlipayDzdInfo;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class CsvUtils {
public static boolean exportCsv(File file, List<String> dataList) {
boolean isSucess = false;
FileOutputStream out = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
try {
out = new FileOutputStream(file);
osw = new OutputStreamWriter(out);
bw = new BufferedWriter(osw);
if (dataList != null && !dataList.isEmpty()) {
for (String data : dataList) {
bw.append(data).append("\r");
}
}
isSucess = true;
} catch (Exception e) {
isSucess = false;
} finally {
if (bw != null) {
try {
bw.close();
bw = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (osw != null) {
try {
osw.close();
osw = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
out = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return isSucess;
}
public static boolean importCsv(String file, int page) {
List<String[]> csvList = new ArrayList<String[]>();
boolean result = false;
int index = 0;
int strat = page * 5000;
int end = (page + 1) * 5000;
try {
CsvReader csvReader = new CsvReader(file, ',', Charset.forName("GBK"));
csvReader.readHeaders();
while (csvReader.readRecord()) {
if ((index >= strat) && (index < end)) {
System.out.println(csvReader.getRawRecord());
csvList.add(csvReader.getValues());
}
if (index == (end - 1)) {
result = true;
break;
}
index++;
}
csvReader.close();
} catch (IOException e) {
System.out.println(e);
}
List<AlipayBill> list = new ArrayList();
for (int row = 0; row < csvList.size(); row++) {
AlipayBill alipayBill = new AlipayBill();
if (csvList.get(row).length == 25) {
list.add(alipayBill);
}
}
System.out.println("读取结束!" + page + ":" + index);
return result;
}
public static void writeContent(List<AlipayDzdInfo> list, String pathName) {
try {
writeFileContent(list,pathName);
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean writeFileContent( List<AlipayDzdInfo> list, String filepath) throws IOException {
Boolean bool = false;
String temp = "";
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null;
try {
File file = new File(filepath);
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buffer = new StringBuffer();
int i;
for (i = 0; (temp = br.readLine()) != null; i++) {
buffer.append(temp);
buffer = buffer.append(System.getProperty("line.separator"));
}
for (int q = 0; q < list.size(); q++) {
buffer.append(list.get(q).getYzpzxh() + ",");
buffer.append(list.get(q).getJyje() + ",");
buffer.append(list.get(q).getSfzh() + ",");
buffer.append(list.get(q).getJfrxm() + ",");
buffer.append(list.get(q).getGldm() + ",");
buffer.append(list.get(q).getXzlx() + " \r\n");
}
fos = new FileOutputStream(file);
pw = new PrintWriter(fos);
pw.write(buffer.toString().toCharArray());
pw.flush();
bool = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
if (fos != null) {
fos.close();
}
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (fis != null) {
fis.close();
}
}
return bool;
}
public static boolean writeFileTitle(AlipayDzdInfo alipayDzdInfo,String filepath) throws IOException {
Boolean bool = false;
String temp = "";
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null;
try {
File file = new File(filepath);
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buffer = new StringBuffer();
int i;
for (i = 0; (temp = br.readLine()) != null; i++) {
buffer.append(temp);
buffer = buffer.append(System.getProperty("line.separator"));
}
buffer.append(alipayDzdInfo.getAllMoney() + "\r\n");
buffer.append(alipayDzdInfo.getAllNumber() + "\r\n");
fos = new FileOutputStream(file);
pw = new PrintWriter(fos);
pw.write(buffer.toString().toCharArray());
pw.flush();
bool = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
if (fos != null) {
fos.close();
}
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (fis != null) {
fis.close();
}
}
return bool;
}
}