import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
/**
* 文件操作
* @author
* @version $Id: CSVUtils.java, v 0.1
2014年7月22日 下午2:19:59 Exp $
*/
public class CSVUtils {
/**
* 生成为CVS文件
* @param exportData
* 源数据List
* @param map
* csv文件的列表头map
* @param outPutPath
* 文件路径
* @param fileName
* 文件名称
* @return
*/
@SuppressWarnings("rawtypes")
public static File createCSVFile(List exportData, LinkedHashMap map, String outPutPath,
String fileName) {
File csvFile = null;
BufferedWriter csvFileOutputStream = null;
try {
File file = new File(outPutPath);
if (!file.exists()) {
file.mkdir();
}
//定义文件名格式并创建
System.out.println("=="+fileName);
csvFile = new File(outPutPath + fileName + ".csv");
File parent = csvFile.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
csvFile.createNewFile();
//csvFile = File.createTempFile(fileName, ".csv", new File(outPutPath));
System.out.println("csvFile:" + csvFile);
// UTF-8使正确读取分隔符","
csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
csvFile), "GBK"), 1024);
System.out.println("csvFileOutputStream:" + csvFileOutputStream);
// 写入文件头部
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
csvFileOutputStream.write((String) propertyEntry.getValue() != null ? new String(
((String) propertyEntry.getValue()).getBytes("GBK"), "GBK") : "");
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
System.out.println(new String(((String) propertyEntry.getValue()).getBytes("GBK"),
"GBK"));
}
csvFileOutputStream.write("\r\n");
// 写入文件内容
for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
Object row = (Object) iterator.next();
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
csvFileOutputStream.write((String) BeanUtils.getProperty(row,
((String) propertyEntry.getKey()) != null? (String) propertyEntry.getKey()
: ""));
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
}
if (iterator.hasNext()) {
csvFileOutputStream.write("\r\n");
}
}
csvFileOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvFileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
// delFolder(path + "/" + tempList[i]);//再删除空文件夹
flag = true;
}
}
return flag;
}
/**
* 测试数据
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
CSVUtils.delAllFile("F:\\download\\aa");//删除目录下的所有文件
System.out.println("deleted");
@SuppressWarnings("rawtypes")
List exportData = new ArrayList<Map>();
@SuppressWarnings("rawtypes")
Map row1 = new LinkedHashMap<String, String>();
row1.put("1", "流水号");
row1.put("2", "外部流水号(账单号) ");
row1.put("3", "支付时间");
row1.put("4", "金额");
row1.put("5", "备注");
row1.put("6", "勾兑结果");
row1.put("7", "");
row1.put("8", "");
row1.put("9", "");
row1.put("10", "");
row1.put("11", "");
row1.put("12", "");
exportData.add(row1);
row1 = new LinkedHashMap<String, String>();
row1.put("1", "20130411000052060000000200023031");
row1.put("2", "1000030001");
row1.put("3", "2013/04/11 20:17");
row1.put("4", "10000");
row1.put("5", ",xxx");
row1.put("6", "success");
row1.put("7", "");
row1.put("8", "");
row1.put("9", "");
row1.put("10", "");
row1.put("11", "");
row1.put("12", "");
exportData.add(row1);
@SuppressWarnings("rawtypes")
LinkedHashMap map = new LinkedHashMap();
map.put("1", "总数");
map.put("2", "11");
map.put("3", "总金额");
map.put("4", "50000");
map.put("5", "成功笔数");
map.put("6", "4");
map.put("7", "成功金额");
map.put("8", "40000");
map.put("9", "失败笔数");
map.put("10", "1");
map.put("11", "失败金额");
map.put("12", "10000");
String path = "F:\\download\\";
String fileName = "test";
File file = CSVUtils.createCSVFile(exportData, map, path, fileName);
String fileName2 = file.getName();
System.out.println("文件名称:" + fileName2);
}
} csv文件生成及删除目录下的所有文件
最新推荐文章于 2021-05-13 10:53:30 发布
本文介绍了一个用于生成CSV文件的Java工具类,该工具类能够从指定的数据源中创建CSV文件,并支持自定义文件路径和文件名。此外,还提供了一个删除指定目录下所有文件的方法。

4504

被折叠的 条评论
为什么被折叠?



