CSVUtils 操作工具类
package utils;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
/**
* CSV操作
*
*
*/
public class CSVUtils {
/**
* 导出
*
* @param file csv文件(路径+文件名),csv文件不存在会自动创建
* @param dataList 数据
* @return
*/
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) {
e.printStackTrace();
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();
}
}
}
if(file.exists()){
file.delete();
}
return isSucess;
}
/**
* 导入
*
* @param file csv文件(路径+文件)
* @return
*/
public static List<String> importCsv(File file){
List<String> dataList=new ArrayList<String>();
BufferedReader br=null;
try {
br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
dataList.add(line);
}
}catch (Exception e) {
}finally{
if(br!=null){
try {
br.close();
br=null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
if(file.exists()){
file.delete();
}
return dataList;
}
/**
* 下载
*
* @param download csv文件(路径+文件)
* @param fileName csv文件名
* @return
*/
public static void downloadCsv(String download,List<String> dataList,HttpServletResponse response){
try {
ServletOutputStream out = response.getOutputStream();
File file = new File(download);
FileOutputStream fos = new FileOutputStream(file);
Writer writer = new OutputStreamWriter(fos, "UTF-8");
for(String data : dataList){
writer.write(data);
}
writer.close();
fos.close();
FileInputStream fis = new java.io.FileInputStream(file);
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(4096);
byte[] cache = new byte[4096];
for (int offset = fis.read(cache); offset != -1; offset = fis.read(cache)) {
byteOutputStream.write(cache, 0, offset);
}
byte[] bt = null;
bt = byteOutputStream.toByteArray();
out.write(bt);
out.flush();
out.close();
fis.close();
if(file.exists()){
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建
*
* @param filePath csv文件(路径+文件名)
* @return
*/
public static void createCsv(String filePath,List<String> dataList){
try {
File file = new File(filePath);
FileOutputStream fos = new FileOutputStream(file);
Writer writer = new OutputStreamWriter(fos, "UTF-8");
for(String data : dataList){
writer.write(data);
}
writer.close();
fos.close();
FileInputStream fis = new java.io.FileInputStream(file);
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(4096);
byte[] cache = new byte[4096];
for (int offset = fis.read(cache); offset != -1; offset = fis.read(cache)) {
byteOutputStream.write(cache, 0, offset);
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}