一个简单的Java文件工具类
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文件工具类的更多相关文章
- 实现一个简单的http请求工具类
OC自带的http请求用起来不直观,asihttprequest库又太大了,依赖也多,下面实现一个简单的http请求工具类 四个文件源码大致如下,还有优化空间 MYHttpRequest.h(类定义, ...
- java文件工具类
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- 一个好的Java时间工具类DateTime
此类的灵感来源于C# 虽然网上有什么date4j,但是jar太纠结了,先给出源码,可以继承到自己的util包中,作为一个资深程序员,我相信都有不少好的util工具类,我也希望经过此次分享,能带动技术大 ...
- 一个简单的Java代码生成工具—根据数据源自动生成bean、dao、mapper.xml、service、serviceImpl
目录结构 核心思想 通过properties文件获取数据源—>获取数据表的字段名称.字段类型等—>生成相应的bean实体类(po.model).dao接口(基本的增删改查).mapper. ...
- 一个简单IP防刷工具类, x秒内最多允许y次单ip操作
IP防刷,也就是在短时间内有大量相同ip的请求,可能是恶意的,也可能是超出业务范围的.总之,我们需要杜绝短时间内大量请求的问题,怎么处理? 其实这个问题,真的是太常见和太简单了,但是真正来做的时候,可 ...
- java http工具类和HttpUrlConnection上传文件分析
利用java中的HttpUrlConnection上传文件,我们其实只要知道Http协议上传文件的标准格式.那么就可以用任何一门语言来模拟浏览器上传文件.下面有几篇文章从http协议入手介绍了java ...
- 自动扫描FTP文件工具类 ScanFtp.java
package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
- Java 实现删除文件工具类
工具代码 package com.wangbo; import java.io.File; /** * 删除目录或文件工具类 * @author wangbo * @date 2017-04-11 1 ...
- Java常用工具类之删除文件
package com.wazn.learn.util; import java.io.File; /** * 删除文件工具类 * @author yangzhenyu * */ public cla ...
随机推荐
- IPython notebook快捷键(Jupyter notebook)
转自“https://blog.csdn.net/eswai/article/details/53642802” 本文整理了神器IPython Notebook(或Jupyter Notebook)的 ...
- [转]1小时内打造你自己的PHP MVC框架
简介 MVC框架在现在的开发中相当流行,不论你使用的是JAVA,C#,PHP或者IOS,你肯定都会选择一款框架.虽然不能保证100%的开发语言都会使用框架,但是在PHP社区当中拥有*多数量的MVC框架 ...
- [转] 一个U盘病毒简单分析
(转自:一个U盘病毒简单分析 - 瑞星网 原文日期:2014.03.25) U盘这个移动存储设备由于体积小.容量大.便于携带等优点,给人们的存储数据带来了很大的便利.但正是由于这种便利,也给病毒有 ...
- SpringBoot 快速开发框架
学习资源:https://ke.qq.com/course/260513(这是Springboot升级版本教程,里面还有一个初级版本的) 1.第一个测试程序,那个覆盖方法加上@Override会报错, ...
- bdflush - 将dirty缓存写回到磁盘的核心守护进程
总览(SYNOPSIS) bdflush [opt] 描述(DESCRIPTION) bdflush 被用来启动核心守护进程将内存中的dirty缓存写到磁盘上.真正清洁工作是一个核心程序完成的. bd ...
- MySQL索引的用处
MySQL索引在MySQL数据库中,可以有效提高查询的效率,尤其是查询数据量非常大时,效果更为明显,往往能使查询速度加快成千上万倍. MySQL索引是很重要的概念,应用的范围非常广.那么,MySQL索 ...
- js 判断访问终端类型
// 判断访问终端类型 var browser = { versions: function() { var u = navigator.userAgent, app = navigator.appV ...
- HR教你面试时怎么谈出高工资
不是任何时候谈钱都会伤感情,比如跟客户谈合同报价,跟房东谈房租,以及面试时和公司HR谈新工作的薪酬待遇. 这事儿一般不需要你先开口.在面试进入尾声的时候,如果HR对你还算满意,通常就会开始问你目前的薪 ...
- wpf Command 携带当前窗口
Command="{Binding GoPayCommand}" CommandParameter="{Binding RelativeSource={RelativeS ...
- No-1.第一个 Python 程序
1. 第一个 HelloWorld 程序 1.1 Python 源程序的基本概念 Python 源程序就是一个特殊格式的文本文件,可以使用任意文本编辑软件做 Python 的开发 Python 程序的 ...