import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

public class FileHelper {

static File file ;

/**

* @删除文件

* input 输入 output 输出

* */

public static boolean deleteFile(String url){

boolean flag = false;

try{

if(null == file){

file = new File(url);

if(file.isFile()){

if(file.canWrite()){

file.delete();

}

flag = true;

}

}else{

if(url == file.getPath()){

file.delete();

flag = true;

}

}

}catch(Exception e){

flag = false ;

e.printStackTrace();

}finally{

file = null;

System.gc();

}

return flag;

}

/**

* 新建目录

* @param folderPath String 如 c:/fqf

* @return boolean

*/

public static void newFolder(String folderPath) {

try {

String filePath = folderPath;

filePath = filePath.toString();

java.io.File myFilePath = new java.io.File(filePath);

if (!myFilePath.exists()) {

myFilePath.mkdir();

}

}

catch (Exception e) {

e.printStackTrace();

}

}

/**

* 复制单个文件

* @param oldPath String 原文件路径 如:c:/fqf.txt

* @param newPath String 复制后路径 如:f:/fqf.txt

* @return boolean

*/

public static void copyFile(String oldPath, String newPath) {

try {

int bytesum = 0;

int byteread = 0;

File oldfile = new File(oldPath);

if (oldfile.exists()) { //文件存在时

InputStream inStream = new FileInputStream(oldPath); //读入原文件

FileOutputStream fs = new FileOutputStream(newPath);

byte[] buffer = new byte[1444];

int length;

while ( (byteread = inStream.read(buffer)) != -1) {

bytesum += byteread; //字节数 文件大小

fs.write(buffer, 0, byteread);

}

inStream.close();

fs.close();

}

}

catch (Exception e) {

e.printStackTrace();

}

}

/**

* 使用文件通道的方式复制文件

* @param s  源文件

* @param t  复制到的新文件

*/

public static void fileChannelCopy(String srcPath,String outpath) {

File s = new File(srcPath);

File t = new File(outpath);

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);//连接两个通道,并且从in通道读取,然后写入out通道

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

fi.close();

in.close();

fo.close();

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 上传文件

* @param upFile 上传文件

* @param targetFile 目标文件

* @param b

* @return

*/

public static boolean fileUpload(File upFile , File targetFile , byte[] b){

InputStream is = null ;

OutputStream os = null ;

try{

is =  new FileInputStream(upFile);

os= new FileOutputStream(targetFile);

int length = 0;

while((length = is.read(b))>0){

os.write(b, 0, length);

}

}catch(Exception e){

e.printStackTrace();

return false;

}finally{

try {

if(null != is){

is.close();

}if(null != os){

os.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return true;

}

/**

* 删除文件

* @param f

* @return

*/

public static boolean deleteFile(File f){

if (f.isFile())

f.delete();

return true;

}

/**

* 删除文件夹

* @param f

* @return

*/

public static boolean deleteDir(File f){

if(f.isDirectory()){

File[] files = f.listFiles();

for(int i=0;i<files.length;i++){

if(files[i].isDirectory()) deleteDir(files[i]);

else deleteFile(files[i]);

}

}

f.delete();

return true ;

}

/**

* 文件下载

* @param filePath 文件路径

* @param fileType 文件类型

* @param downName 下载文件名称

* @param response 响应

*/

public static void downLoadFile(String filePath,String fileType ,String downName,HttpServletResponse response){

OutputStream out = null;// 创建一个输出流对象

String headerStr = downName;

try{

InputStream is=new FileInputStream(filePath);

out = response.getOutputStream();//

headerStr =new String( headerStr.getBytes("gb2312"), "ISO8859-1" );//headerString为中文时转码

response.setHeader("Content-disposition", "attachment; filename="+ headerStr+"."+fileType);// filename是下载的xls的名,建议最好用英文

response.setContentType("application/msexcel;charset=GB2312");// 设置类型

response.setHeader("Pragma", "No-cache");// 设置头

response.setHeader("Cache-Control", "no-cache");// 设置头

response.setDateHeader("Expires", 0);// 设置日期头

// 输入流 和 输出流

int len = 0;

byte[] b = new byte[1024];

while ((len = is.read(b)) != -1) {

out.write(b, 0, len);

}

is.close();

out.flush();

out.close();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (out != null) {

out.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

FileHelper-文件操作工具的更多相关文章

  1. Code片段 : .properties属性文件操作工具类 & JSON工具类

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 j ...

  2. [svc]find+xargs/exec重命名文件后缀&文件操作工具小结

    30天内的文件打包 find ./test_log -type f -mtime -30|xargs tar -cvf test_log.tar.gz awk运算-解决企业统计pv/ip问题 find ...

  3. 文件操作工具类: 文件/目录的创建、删除、移动、复制、zip压缩与解压.

    FileOperationUtils.java package com.xnl.utils; import java.io.BufferedInputStream; import java.io.Bu ...

  4. JAVA文件操作工具类(读、增、删除、复制)

    使用JAVA的JFinal框架 1.上传文件模型类UploadFile /** * Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com). * ...

  5. Android文件操作工具类(转)

    Android文件操作工具类(转)  2014/4/3 18:13:35  孤独的旅行家  博客园 这个工具类包含Android应用开发最基本的几个文件操作方法,也是我第一次发博客与大家分享自己写的东 ...

  6. 小米开源文件管理器MiCodeFileExplorer-源码研究(4)-文件操作工具类FileOperationHelper

    文件操作是非常通用的,注释都写在源代码中了,不多说~需要特别说明的是,任务的异步执行和IOperationProgressListener.拷贝和删除等操作,是比较费时的,采用了异步执行的方式~ An ...

  7. C#文件操作工具类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  8. Java文件操作工具类(复制、删除、重命名、创建路径)

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  9. 7、Python文件操作工具 openpyxl 工具 2

    创建一个工作簿 使用openpyxl没有必要先在系统中新建一个.xlsx,我们需要做的只需要引入Workbook这个类,接着开始调用它. >>> from openpyxl impo ...

  10. 6、Python文件操作工具 openpyxl 工具

    #-*- coding:utf-8 -* from  openpyxl.reader.excel  import  load_workbook import  MySQLdb import  time ...

随机推荐

  1. CentOS6.7安装部署php5(详解安装选项与主配置文件)

    模块安装---PHP 编译环境:gcc  gcc-c++   pcre-devel  openssl-devel   libxml2   libxml2-devel   bzip   bzip-dev ...

  2. Kubernetes 监控

    1. Weave Scope Weave Scope 容器地图 创建 Kubernetes 集群并部署容器化应用只是第一步.一旦集群运行起来,我们需要确保一起正常,所有必要组件就位并各司其职,有足够的 ...

  3. python的模块放在哪里

    python程序中使用 import XXX 时,python解析器会在当前目录.已安装和第三方模块中搜索 xxx,如果都搜索不到就会报错. 使用sys.path.append()方法可以临时添加搜索 ...

  4. 折腾deepin修改终端语言

    原创作品,作者是博客园sogeisetsu,转载请注明来源sogeisetsu.cnblogs.com 唉-都怪当初没学扎实,改个终端语言花费了半天. 首先,介绍一下我的情况 有两个用户,一个是roo ...

  5. 第四篇 -- XPath

    .Net框架下的System.Xml.XPath命名空间提供了一系列的类,允许你应用XPath数据模式查询和展示XML文档数据. 一.XPath介绍 XPath有七种类型的节点:元素.属性.文本.命名 ...

  6. test20191020 往复

    往复 Coldhac 做不出题了,他在长为 n 的走廊里走来走去.从位置 1 开始, 每次他会向前走长为 i ∈ [1, k] 的一步(不能超出走廊的范围),直至到达位 置 n. 在想出正解前,Col ...

  7. wordpress时间函数the_time() 实例解读

    wordpress the_time()时间函数想必大家多多少少都会用到,但是要自定义一些时间相对没那么熟悉了,随ytkah一起来看看吧.我们知道时间函数基础调用是<?php the_time( ...

  8. Hbase 分页设计

    hbase 数据获取方式 直接根据 rowkey 查找,速度最快 scan,指定 startrowkey.endrowkey 和 limit获取数据,在 rowkey 设计良好的情况下,效率也不错 全 ...

  9. IMP本质上是一个通用的函数指针

    IMP:通用的函数指针 /// A pointer to the function of a method implementation. #if !OBJC_OLD_DISPATCH_PROTOTY ...

  10. 信噪比(signal-to-noise ratio)

    SNR或S/N,又称为讯噪比.是指一个电子设备或者电子系统中信号与噪声的比例.这里面的信号指的是来自设备外部需要通过这台设备进行处理的电子信号,噪声是指经过该设备后产生的原信号中并不存在的无规则的额外 ...