/**
* Desc : 生成为CVS文件
* User : RICK
* @param data 源数据List
* @param map csv文件的列表头map
* @param outPutPath 文件路径
* @param fileName 文件名称
* Time : 2017/10/20 13:50
*/
@SuppressWarnings("rawtypes")
public static File createCVSFile(List data, LinkedHashMap map, String outPutPath, String fileName){
File csvFile = null;
BufferedWriter bufferedWriter = null;
try {
File file = new File(outPutPath);
if (!file.exists()){
file.mkdir();
}
//定义文件名格式并创建
csvFile = File.createTempFile(fileName, ".cvs", new File(outPutPath));
System.out.println("csvFile:" + csvFile);
// UTF-8使正确读取分隔符","
bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8"), 1024);
// 写入文件头部
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
bufferedWriter.write((String) propertyEntry.getValue() != null ? new String(
((String) propertyEntry.getValue()).getBytes("UTF-8"), "UTF-8") : "");
if (propertyIterator.hasNext()) {
bufferedWriter.write(",");
}
System.out.println(new String(((String) propertyEntry.getValue()).getBytes("UTF-8"),
"UTF-8"));
}
bufferedWriter.write("\r\n");
// 写入文件内容
for (Iterator iterator = data.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();
bufferedWriter.write((String) BeanUtils.getProperty(row,
((String) propertyEntry.getKey()) != null? (String) propertyEntry.getKey()
: ""));
if (propertyIterator.hasNext()) {
bufferedWriter.write(",");
}
}
if (iterator.hasNext()) {
bufferedWriter.write("\r\n");
}
}
bufferedWriter.flush();
} catch(Exception e){
e.printStackTrace();
} finally {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
} /**
* Desc : 下载文件
* User : RICK
* @param response
* @param csvFilePath 文件路径
* @param fileName 文件名称
* Time : 2017/10/20 13:53
*/
public static void exportFile(HttpServletResponse response, String csvFilePath, String fileName)
throws IOException {
response.setContentType("application/csv;charset=GBK");
response.setHeader("Content-Disposition",
"attachment; filename=" + new String(fileName.getBytes("GBK"), "ISO8859-1"));
//URLEncoder.encode(fileName, "UTF-8")
InputStream in = null;
try {
in = new FileInputStream(csvFilePath);
int len = 0;
byte[] buffer = new byte[1024];
response.setCharacterEncoding("GBK");
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer)) > 0) {
//out.write(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF });
out.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
System.out.println(e);
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
} /**
* Desc : 删除该目录filePath下的所有文件
* User : RICK
* @param filePath 文件目录路径
* Time : 2017/10/20 13:53
*/
public static void deleteFiles(String filePath) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
files[i].delete();
}
}
}
} /**
* Desc : 删除单个文件
* User : RICK
* @param filePath 文件目录路径
* @param fileName 文件名称
* Time : 2017/10/20 13:54
*/ public static void deleteFile(String filePath, String fileName) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
if (files[i].getName().equals(fileName)) {
files[i].delete();
return;
}
}
}
}
}public static void main(String[] args) {
List exportData = new ArrayList<Map>();
Map row1 = new LinkedHashMap<String, String>();
row1.put("1", "rick");
row1.put("2", "男");
row1.put("3", "20");
row1.put("4", "14000000000");
exportData.add(row1);
row1 = new LinkedHashMap<String, String>();
row1.put("1", "anna");
row1.put("2", "女");
row1.put("3", "23");
row1.put("4", "180123456789");
exportData.add(row1);
LinkedHashMap map = new LinkedHashMap();
map.put("1", "姓名");
map.put("2", "性别");
map.put("3", "年龄");
map.put("4", "手机号"); String path = "D:/export/";
String fileName = DateUtil.getYearMonthDay() + Constant.CharacterType.underline;
File file = CvsUtil.createCVSFile(exportData, map, path, fileName);
String fileName2 = file.getName();
System.out.println("文件最终名称:" + fileName2); }

170808、生成为CVS文件的更多相关文章

  1. BAT 快速删除CVS文件和拷贝最近修改文件的目录结构

    相信大家在操作大量文件的的时候,经常会遇到一些手动很难操作的情况 比如有CVS版本控制下每个文件夹下都有一个CVS文件夹,一个个手工删除肯定很费劲,我们都是懒人,还是用工具解决吧.不用重新写程序,直接 ...

  2. CVS 文件自动移 tag 的 Python 脚本

    CVS 文件自动移 tag 的 Python 脚本 背景 工作中使用的版本管理工具是 CVS,在两次发布中,如果修改的文件比较少,会选择用移 Tag 的方式来生成一个新 Tag 发布.文件比较少的情况 ...

  3. matlab读取cvs文件的几种方法

    matlab读取CVS文件的几种方法: 1,实用csvread()函数   csvread()函数有三种使用方法: 1.M = csvread('filename')2.M = csvread('fi ...

  4. wps直接打开CVS文件会把长串数字订单号最后4位变为0

    WPS打开CVS文件,发现里面的长串数字订单号后4位全变成0了,而且是以科学计数法来显示了 上网查了下,不能直接打开,得先在WPS(OFFICE中也一样)中新建一个空白表格,然后用菜单里的数据导入功能 ...

  5. access 如何导出 cvs 文件?

    三部曲 1 access 数据表导出 excel 表格 2 excel 另存为 *.cvs 格式文件 3 数据库导入 *.cvs 文件

  6. QCustomplot使用分享(八) 绘制图表-加载cvs文件

    目录 一.概述 二.效果图 三.源码讲解 1.源码结构 2.头文件 3.移动游标 4.设置坐标轴矩形个数 5.添加图表数据 6.设置折线图类型 6.其他函数 四.测试方式 1.测试工程 2.测试文件 ...

  7. 使用notepad++/excle快速将cvs文件转换为insert语句技巧以及注意点

    使用notepad++/excle快速将cvs文件转换为insert语句技巧以及注意点 业务场景 最近nc项目经理从第三方弄来了一个300w行的csv文件,让导入数据库做处理,出现了下列问题: csv ...

  8. python py生成为pyc文件

    生成单个pyc文件 python就是个好东西,它提供了内置的类库来实现把py文件编译为pyc文件,这个模块就是 py_compile 模块. 使用方法非常简单,如下所示,直接在idle中,就可以把一个 ...

  9. cvs 文件无法上传debug

    当时文件始终上传不成功时(一般先update后commit): cvs update filename report:move away filename ,it is in the way cvs ...

随机推荐

  1. Java 阻塞

    对于用ServerSocket 及 Socket 编写的服务器程序和客户程序, 他们在运行过程中常常会阻塞. 例如, 当一个线程执行 ServerSocket 的accept() 方法时, 假如没有客 ...

  2. 获取作为 URL 部署清单的位置。

    ActivationContext ac = AppDomain.CurrentDomain.ActivationContext; ApplicationIdentity ai = ac.Identi ...

  3. MVC下载远程文件流(WebClient)

    public ActionResult DownLoad_File() { return File(ScLiu(PathUrl), "application/octet-stream&quo ...

  4. Cookie文件格式解析

    原文参考:http://blog.csdn.net/lixianlin/article/details/2738229 1.Cookie文件的实质 Cookie实际上是Web服务端与客户端(典型的是浏 ...

  5. Unity文件操作路径

    Unity3D中的资源路径: Application.dataPath:此属性用于返回程序的数据文件所在文件夹的路径.例如在Editor中就是Assets了. Application.streamin ...

  6. 安装并配置ROS环境1

    ros学习之路(原创博文,转载请标明出处-周学伟http://www.cnblogs.com/zxouxuewei/) 一.ros核心教程    1.安装并配置ROS环境: 注意: 学习这节课之前请按 ...

  7. 记XX2013届优秀毕业生评选(请重视在公司展现自己,重视业绩參评过程,非技术贴)

    本文不是什么技术贴.仅仅是作为一名码农,在公司发展中遇到"參评"中的一个分享,希望对大家有帮助.毕竟,升职加薪这样的事情,你须要自己去争取,须要获得领导和同事的认可.. . .考虑 ...

  8. 超全面的JavaWeb笔记day03<JS对象&函数>

    1.js的String对象(****) 2.js的Array对象 (****) 3.js的Date对象 (****) 获取当前的月 0-11,想要得到准确的月 +1 获取星期时候,星期日是 0 4.j ...

  9. Java精选笔记_Tomcat开发Web站点

    Tomcat开发Web站点 Web开发的相关知识 B/S架构和C/S架构 C/S架构是Client/Server的简写,也就是客户机/服务器端的交互.常见应用 : QQ. 迅雷. 360. 旺旺等 B ...

  10. Android DOM解析XML示例程序

    DOM方式解析xml是先把xml文档都读到内存中,然后再用DOM API来访问树形结构,并获取数据的.DOM比较符合人的思维模式,但是其对内存的消耗比较大. activity_main.xml < ...