一个简单的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 ...
随机推荐
- android中使用图文并茂的按钮
代码: <LinearLayout android:orientation="horizontal" android:layout_width="match_par ...
- SCHTASKS /CREATE
SCHTASKS "/" 这个符号前要加个空格才能运行成功 ,搞半天!
- python_函数的可变参数
def test(*args,**kwargs): print(args) print(kwargs) test(1,2,3,x=1,y=2) 运行结果: *args称为positional argu ...
- hystrix 解决服务雪崩效应
1.服务雪崩效应 默认情况下tomcat只有一个线程池去处理客户端发送的所有服务请求,这样的话在高并发情况下,如果客户端所有的请求堆积到同一个服务接口上, 就会产生tomcat的所有线程去处理该服务接 ...
- POJ中和质数相关的三个例题(POJ 2262、POJ 2739、POJ 3006)
质数(prime number)又称素数,有无限个.一个大于1的自然数,除了1和它本身外,不能被其他自然数整除,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数. 最小的质数 ...
- vue 模板下只能有一个跟节点 根节点一定要是个div
<template> <div>简单说就是里面只能有一个跟的div button1.vue <template> <div> <Button> ...
- Eclipse使用入门指南及技巧
Java是必须的 安装一个JDK就可以了,比如jdk-6u39-windows-x64.exe,安装完毕,会自行安装JRE. 如果不用IDE,这个时候用记事本也是可以写程序,然后用javac编译, ...
- P1759 通天之潜水(不详细,勿看)(动态规划递推,组合背包,洛谷)
题目链接:点击进入 题目分析: 简单的组合背包模板题,但是递推的同时要刷新这种情况使用了哪些物品 ac代码: #include<bits/stdc++.h> using namespace ...
- 零基础入门学习Python(6)--Python之常用操作符
前言 Python当中常用操作符,有分为以下几类.幂运算(**),正负号(+,-),算术操作符(+,-,*,/,//,%),比较操作符(<,<=,>,>=,==,!=),逻辑运 ...
- Node.js中的Buffer
Buffer介绍 为什么要用Buffer? 在Node/ES6 出现之前,前端工程师只需要进行一些简单的额字符串或者ODM操作就可以满足业务需求了,所有对二进制数据比较陌生. 在node出现之后,前端 ...