/**
* 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. 关于Unity中UI中的Button节点以及它的Button组件

    Button是最常用的UI节点,包含的组件有 1.Image组件 显示Button的纹理,把Image贴图拖进Image组件中后,记得点击Set Native Size,显示贴图原始大小 2.Butt ...

  2. 【F12】网络面板

    使用网络面板了解请求和下载的资源文件并优化网页加载性能 (1)网络面板基础 测量资源加载时间 使用 Network 面板测量您的网站网络性能. Network 面板记录页面上每个网络操作的相关信息,包 ...

  3. Android BitmapFactory

    android BitmapFactory BitmapFactory是一个工具类,用于从不同数据源解析,创建Bitmap对象.bitmap类代表位图. BitmapFactory常用方法 stati ...

  4. 类加载器详解 (转至http://blog.csdn.net/jiangwei0910410003/article/details/17733153)

    首先来了解一下字节码和class文件的区别: 我们知道,新建一个java对象的时候,JVM要将这个对象对应的字节码加载到内存中,这个字节码的原始信息存放在classpath(就是我们新建Java工程的 ...

  5. ThinkPHP Mongo驱动update方法支持upsert参数

    Mongo数据库update操作有一个相对于Mysql的关键特性,它可以使用upsert模式,当更新的数据不存在时,直接插入,但是ThinkPHP的Mongo驱动居然不支持这一特性,没办法,自力更生了 ...

  6. JavaScript 事件参考手册

    事件通常与函数配合使用,这样就可以通过发生的事件来驱动函数执行. 事件句柄 HTML 4.0 的新特性之一是有能力使 HTML 事件触发浏览器中的动作(action),比如当用户点击某个 HTML 元 ...

  7. RAC:Oracle11gR2:群集的起、停、状态查询

    一:查看群集的状态 1.0.1 使用crsctl status resource [-t] 1.0.2 使用crs_stat [-t] 1.0.1 使用srvctl status <obj> ...

  8. 设计模式工具:UML基础

    类图 矩形框                                          -类Class 第一层                                          ...

  9. cesium可视化空间数据2

    圆柱圆锥体 <!DOCTYPE html> <html> <head> <!-- Use correct character set. --> < ...

  10. linux定时任务cron 安装配置

    名词解释: cron是服务名称,crond是后台进程,crontab则是定制好的计划任务表. 软件包安装: 要使用cron服务,先要安装vixie-cron软件包和crontabs软件包,两个软件包作 ...