一个简单的Java文件工具类
2024-08-29 05:57:37
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);
}*/ }
最新文章
- 深入解析Sqlite的完美替代者,android数据库新王者——Realm
- noip2006 2^k进制数
- 30道四则运算<;2>;
- 推荐第三方Oracle客户端查询工具
- xe5 android listbox的 TMetropolisUIListBoxItem
- eclipse配置mahout
- mysql基本知识
- leetcode面试准备:Triangle
- 使用WMI来连接远端计算机
- 201521123071 《JAVA程序设计》第九周学习总结
- Atitit uke公司简介与基本制度建设草案v11 r66 .docx
- aes 和 Md5 分析
- Redis学习-常用命令
- Nginx反向代理tomcat返回400 bad request
- mac date
- SpringBoot入门示例
- java中的排序--排序容器_TreeSet与TreeMap
- 基于thinkphp的在线编辑器kindeditor-v4.1.3
- wps 批量调整图片大小 宏
- 漫谈四种神经网络序列解码模型【附示例代码】 glimpse attention