最近研究压缩csv文件出力,根据网上资料,整理了一套用不导包的例子。
引包
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream;
调用
//临时csv作成 File csvFile1 = CsvCreator.out(entities); String url2 = csvFile1.getPath(); url2 = url2.substring(0, url2.length() - 3); fileChannelCopy(csvFile1.getPath(), url2 + ".csv"); // ZIP压缩包 zip(url + ".zip", new File(url + ".csv")); StringBuilder uri = new StringBuilder(); uri.append(url); uri.append(".zip"); // 下载 downloadFile(request, response, uri); // 删除临时文件 deleteFile(url + ".csv"); deleteFile(url + ".zip");
文件复制
private void fileChannelCopy(String ChangeBeforeName, String ChangeAfterName) { File s = new File(ChangeBeforeName); File t = new File(ChangeAfterName); FileInputStream fi = null; FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try { fi = new FileInputStream(s); fo = new FileOutputStream(t); in = fi.getChannel(); out = fo.getChannel(); in.transferTo(0, in.size(), out); } catch (IOException e) { e.printStackTrace(); } finally { try { fi.close(); in.close(); fo.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
压缩文件
private void zip(String zipFileName, File inputFile) throws Exception { System.out.println("Start..."); ZipOutputStream out = new ZipOutputStream(new FileOutputStream( zipFileName)); BufferedOutputStream bo = new BufferedOutputStream(out); zip(out, inputFile, inputFile.getName(), bo); bo.close(); out.close(); System.out.println("End..."); } private void zip(ZipOutputStream out, File f, String base, BufferedOutputStream bo) throws Exception { if (f.isDirectory()) { File[] fl = f.listFiles(); if (fl.length == 0) { out.putNextEntry(new ZipEntry(base + "/")); System.out.println(base + "/"); } for (int i = 0; i < fl.length; i++) { zip(out, fl[i], base + "/" + fl[i].getName(), bo); } } else { out.putNextEntry(new ZipEntry(base)); System.out.println(base); FileInputStream in = new FileInputStream(f); BufferedInputStream bi = new BufferedInputStream(in); int b; while ((b = bi.read()) != -1) { bo.write(b); } bi.close(); in.close(); } }
下载
private void downloadFile(HttpServletRequest request, HttpServletResponse response, StringBuilder uri) throws IOException { StringBuffer filename = new StringBuffer(); filename.append(uri); File file = new File(filename.toString()); StringBuffer sb = new StringBuffer(); String name = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip"; sb.append("attachment; filename=").append(name); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); response.setContentType("application/x-msdownload;charset=UTF-8"); response.setHeader("Content-Disposition", new String(sb.toString() .getBytes(), "Windows-31J")); FileInputStream inputStream = new FileInputStream(file); OutputStream outputStream = response.getOutputStream(); byte[] buffer = new byte[1024]; int i = -1; while ((i = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, i); } outputStream.flush(); outputStream.close(); inputStream.close(); }
删除临时文件
private void deleteFile(String sPath) { File file = new File(sPath); if (file.isFile() && file.exists()) { file.delete(); } return; }
相关推荐
java开发 csv文件出力
VB.NET Web开发中CSV出力实例
该项目将使用Cobol Copybook将Cobol数据文件转换为Csv文件或从中转换为Csv文件。
WPF_随机生成数据并以CSV形式出力,还有两个按钮的特效,适合WPF初学者。
海上风电出力预测的用电数据分为训练组和测试组两大类,主要包括风电场基本信息、气象变量数据和实际功率数据三个部分。风电场基本信息主要是各风电场...数据集文件的格式为csv格式。A榜包括2个训练集和2个测试集数据。
log4j文件修改出力位置即可,放在Main文件同一个包下。
2012年,全年的风电场风速数据(每5min一测)共105121个风速数据及出力数据[m/s],表头Year Month Day Hour Minute power (MW) wind speed at 100m (m/s)
可以计算一天的光伏出力情况,改变数据以改变光伏出力
电力系统风机出力模型,利用威布尔分布构建风机出力,加入电力系统中可得近似分析,对新能源接入电力系统研究提出新思路
蒙特卡洛模拟风电场随机出力,产生多种场景
离心泵是化工、电力生产中的重要输送设备,凝结水离心泵(凝结水泵)是电厂中的...分析了凝结水泵发生出力不足的原因,并通过对凝结水泵入口蝶阀进行改造,消除了出力不足的设备缺陷,提高了凝结水泵运行的安全性和经济性。
基于集群风电出力均值、标准差、偏度和峰度4个统计性指标,构建表征集群风电出力概率分布的皮尔逊族模型,模拟集群风电场出力时序数据,进而得到集群风电场出力曲线。基于对区域典型风电集群历史出力数据的分析,...
C语言文件操作的基本语言规范,输入输出流,如何读取和存储文件。
首先,程序导入了一个名为"windpower.csv"的数据文件,其中包含了风电场的出力数据。然后,程序绘制了机组1和机组2的频率直方图,以及原始数据的二元频数直方图。 接下来,程序对机组1和机组2的数据进行了正态性...
蒙塔卡罗模拟,生成风电和光伏出力场景并进行削减
matlab神经网络,主要用于预测光伏出力,输入为温度湿度等因素,输出为光伏出力
提高风电出力的预测精度可降低含高渗透率风电电力 系统调度、优化、规划等策略的保守性和控制策略的复杂性。 该文在分析风电出力历史数据与气象因素关系的基础上,建 立了基于风电出力数据驱动的短期风电功率预估–...
用HOMER软件画出的某地区一年每小时的光照,将其导出为文本格式,可导入到MATLAB中供研究人员使用
分析电力系统风电出力数据的状态转移概率。在得到两三年的风电场发电出力数据后,可用该程序分析风电出力的具体变化,以及风电出力在相邻时刻不同状态之间的转移概率。
#资源达人分享计划#