package com.xyworkroom.ntko.util;

 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; /**
* 文件处理工具类
*
* @author xmq
*/
public class FileUtil { /**
* 下载文件
* @param response
* @param filePat 包括文件名如:c:/a.txt
* @param fileName 文件名如:a.txt
*/
public static void downFile(HttpServletResponse response,String filePath,String fileName){
try {
response.setCharacterEncoding("gkb");
response.setContentType("text/plain");
response.setHeader("Location",fileName);
response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("gb2312"),"ISO8859-1"));
FileInputStream fis=new FileInputStream(filePath);
OutputStream os=response.getOutputStream();
byte[] buf=new byte[1024];
int c=0;
while((c=fis.read(buf))!=-1){
os.write(buf, 0, c);
}
os.flush();
os.close();
if(fis!=null){
fis.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 检查文件是否存在,存在返回true
* @param destFileName
* @return
*/
public static boolean checkFileIsExists(String destFileName){
File file = new File(destFileName);
if (file.exists()) {
return true;
}else{
return false;
}
}
/**
* 复制文件
* @param source
* @param dest
* @throws IOException
*/
public static void copyFile(File source, File dest){
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf))>-1) {
output.write(buf, 0, bytesRead);
}
output.close();
input.close();
}catch(Exception e){
e.printStackTrace();
}
} /**
* 把输入流保存到指定文件
* @param source
* @param dest
* @throws IOException
*/
public static void saveFile(InputStream source, File dest){
InputStream input = null;
OutputStream output = null;
try {
input =source;
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf))>-1) {
output.write(buf, 0, bytesRead);
}
output.close();
input.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 创建文件
*/
public static boolean createFile(String destFileName) {
File file = new File(destFileName);
if (file.exists()) {
return false;
}
if (destFileName.endsWith(File.separator)) {
return false;
}
if (!file.getParentFile().exists()) {
if (!file.getParentFile().mkdirs()) {
return false;
}
}
try {
if (file.createNewFile()) {
return true;
} else {
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
} /**
* 创建目录
*/
public static boolean createDir(String destDirName) {
File dir = new File(destDirName);
if (dir.exists()) {
return false;
}
if (!destDirName.endsWith(File.separator))
destDirName = destDirName + File.separator;
if (dir.mkdirs()) {
return true;
} else {
return false;
}
} /**
* 根据路径删除指定的目录或文件,无论存在与否
*/
public static boolean DeleteFolder(String sPath) {
boolean flag = false;
File file = new File(sPath);
if (!file.exists()) {
return flag;
} else {
if (file.isFile()) {
return deleteFile(sPath);
} else {
return deleteDirectory(sPath);
}
}
} /**
* 删除单个文件
*/
public static boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
} /**
* 删除目录(文件夹)以及目录下的文件
*/
public static boolean deleteDirectory(String sPath) {
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag)
break;
} else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag)
break;
}
}
if (!flag)
return false;
if (dirFile.delete()) {
return true;
} else {
return false;
}
} /*public static void main(String[] args) {
String dir = "D:\\sgtsc_files\\393\\02\\";
createDir(dir);
String filename = "test1.txt";
String subdir = "subdir";
createDir(dir + subdir);
createFile(dir + filename);
createFile(dir + subdir + filename);
DeleteFolder(dir);
}*/ }

一个简单的Java文件工具类的更多相关文章

  1. 实现一个简单的http请求工具类

    OC自带的http请求用起来不直观,asihttprequest库又太大了,依赖也多,下面实现一个简单的http请求工具类 四个文件源码大致如下,还有优化空间 MYHttpRequest.h(类定义, ...

  2. java文件工具类

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  3. 一个好的Java时间工具类DateTime

    此类的灵感来源于C# 虽然网上有什么date4j,但是jar太纠结了,先给出源码,可以继承到自己的util包中,作为一个资深程序员,我相信都有不少好的util工具类,我也希望经过此次分享,能带动技术大 ...

  4. 一个简单的Java代码生成工具—根据数据源自动生成bean、dao、mapper.xml、service、serviceImpl

    目录结构 核心思想 通过properties文件获取数据源—>获取数据表的字段名称.字段类型等—>生成相应的bean实体类(po.model).dao接口(基本的增删改查).mapper. ...

  5. 一个简单IP防刷工具类, x秒内最多允许y次单ip操作

    IP防刷,也就是在短时间内有大量相同ip的请求,可能是恶意的,也可能是超出业务范围的.总之,我们需要杜绝短时间内大量请求的问题,怎么处理? 其实这个问题,真的是太常见和太简单了,但是真正来做的时候,可 ...

  6. java http工具类和HttpUrlConnection上传文件分析

    利用java中的HttpUrlConnection上传文件,我们其实只要知道Http协议上传文件的标准格式.那么就可以用任何一门语言来模拟浏览器上传文件.下面有几篇文章从http协议入手介绍了java ...

  7. 自动扫描FTP文件工具类 ScanFtp.java

    package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  8. Java 实现删除文件工具类

    工具代码 package com.wangbo; import java.io.File; /** * 删除目录或文件工具类 * @author wangbo * @date 2017-04-11 1 ...

  9. Java常用工具类之删除文件

    package com.wazn.learn.util; import java.io.File; /** * 删除文件工具类 * @author yangzhenyu * */ public cla ...

随机推荐

  1. ibatis 的sqlMap 的xml 关注点

    1.当有特殊字符时候需要保持原状 eg:特殊字符  <> 错误:t.name<>' '   会报The content of elements must consist of ...

  2. win7+idea+maven搭建spark源码阅读环境

    1.参考. 利用IDEA工具编译Spark源码(1.60~2.20) https://blog.csdn.net/He11o_Liu/article/details/78739699 Maven编译打 ...

  3. 重构31-Replace conditional with Polymorphism(多态代替条件)

    多态(Polymorphism)是面向对象编程的基本概念之一.在这里,是指在进行类型检查和执行某些类型操作时,最好将算法封装在类中,并且使用多态来对代码中的调用进行抽象. public class O ...

  4. mvc框架 与vuex的介绍

    应用级的状态集中放在store中: 改变状态的方式是提交mutations,这是个同步的事物: 异步逻辑应该封装在action中. const vuex_store = new Vuex.store( ...

  5. 【译】x86程序员手册40-10.5初始化的例子

    10.5 Initialization Example初始化的例子 译注:本来想把这个例子全部注释完,但由于对intel汇编实不熟悉,有太多的伪指令,本人也是免强看懂,所以就不再做翻译了. $TITL ...

  6. spring cloud Bug之was unable to refresh its cache! status = Cannot execute request on any known server

    可能原因: 1.application.yml server: port: 10001spring: application: name: microservice-consumer-movieeur ...

  7. 第3节 hive高级用法:14、hive的数据压缩

    六.hive的数据压缩 在实际工作当中,hive当中处理的数据,一般都需要经过压缩,前期我们在学习hadoop的时候,已经配置过hadoop的压缩,我们这里的hive也是一样的可以使用压缩来节省我们的 ...

  8. Java代码的编译和执行

    Java代码编译和执行的整个过程包含了以下三个重要的机制: (1)Java源码编译机制 (2)类加载机制 (3)类执行机制 1.Java代码编译是由Java源码编译器来完成,流程图: Java 源码编 ...

  9. sqllite相关总结

    一,sqlite 简介 前面写了一篇博文讲如何在 C# 中使用 ADO 访问各种数据库,在移动开发和嵌入式领域也有一个轻量级的开源关系型数据库-sqlite.它的特点是零配置(无需服务器),单磁盘文件 ...

  10. linux系统查看网络连接情况

    netstat命令状态说明: CLOSED                      没有使用这个套接字[netstat 无法显示closed状态] LISTEN 套接字正在监听连接[调用listen ...