package sy.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
public class FileUtil {
/**
* 在系统临时文件夹下面创建一个目录
*
* @param directory
* @return String 临时路径文件夹字符串
*/
public static String getTempFolderUnderAvidmTempFolder(String directory) {
String tempPath = System.getProperty("java.io.tmpdir");
if (!tempPath.endsWith(File.separator)) {
tempPath += File.separator;
}
tempPath += directory;
File fTemp = new File(tempPath);
if (!fTemp.exists()) {
fTemp.mkdir();
}
return tempPath;
}
/**
* 检查一个字符串是否是已存在的目录
*/
public static boolean checkFolderPath(String toCheckString) {
boolean result = false;
File tempFile = new File(toCheckString);
if (tempFile.exists()) {
if (tempFile.isDirectory()) {
result = true;
}
}
return result;
}
/**
* 将文件的字节表示的大小转化成字符串 将字节转换为KB或MB
*
* @param fileSize
* 准换的文件大小(字节)
* @return String
*/
public static String getFileSize(String fileSize) {
int docSize;
int kSize, mSize;
int kOne = 1024;
int mOne = 1024 * 1024;
String retSize;
docSize = new Integer(fileSize).intValue();
kSize = docSize / kOne;
mSize = kSize / kOne;
if (mSize > 0) {
retSize = new Integer(mSize).toString();
retSize += ".";
retSize += new Integer((docSize - mSize * mOne) / mOne).toString();
retSize += "MB";
return retSize;
} else if (kSize > 0) {
retSize = new Integer(kSize).toString();
retSize += ".";
retSize += new Integer((docSize - kSize * kOne) / kOne).toString();
retSize += "KB";
return retSize;
} else {
retSize = new Integer(docSize).toString();
retSize += "B";
return retSize;
}
}
/**
* 复制单个文件
*
* @param oldPath
* 原文件地址,如:c:/a.txt
* @param newPath
* 新文件地址,如:d:/b.txt
* @return
*/
public static boolean copyFile(String oldPath, String newPath) {
InputStream inStream = null;
FileOutputStream fs = null;
boolean result = false;
try {
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { // 文件存在时
inStream = new FileInputStream(oldPath); // 读入原文件
fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
fs.write(buffer, 0, byteread);
}
result = true;
} else {
result = false;
}
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
} finally {
try {
fs.close();
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 复制整个文件夹内容
*
* @param oldPath
* 原文件夹路径,如:c:/t1
* @param newPath
* 新文件夹路径,如:d:/t2
*/
public static void copyFolder(String oldPath, String newPath) {
FileInputStream input = null;
FileOutputStream output = null;
try {
(new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File a = new File(oldPath);
String[] file = a.list();
File temp = null;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}
if (temp.isFile()) {
input = new FileInputStream(temp);
output = new FileOutputStream(newPath + File.separator
+ (temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
}
if (temp.isDirectory()) {// 如果是子文件夹
copyFolder(oldPath + File.separator + file[i], newPath
+ File.separator + file[i]);
}
}
} catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
} finally {
try {
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 新建目录, 如:c:/abc
*
* @param folderPath
*/
public static void newFolder(String folderPath) {
try {
String filePath = folderPath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
if (!myFilePath.exists()) {
myFilePath.mkdir();
}
} catch (Exception e) {
System.out.println("新建目录操作出错");
e.printStackTrace();
}
}
/**
* 新建文件
*
* @param filePathAndName
* 文件路径(包括文件路径+文件名称),如:c:/fqf.txt
* @param fileContent
* 文件内容
*/
public static void newFile(String filePathAndName, String fileContent) {
FileWriter resultFile = null;
try {
String filePath = filePathAndName;
filePath = filePath.toString();
File myFilePath = new File(filePath);
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
String strContent = fileContent;
myFile.println(strContent);
} catch (Exception e) {
System.out.println("新建文件操作出错");
e.printStackTrace();
} finally {
try {
resultFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 删除文件
*
* @param filePathAndName
* 文件路径(包括文件路径+文件名称),如:c:/fq.txt
*/
public static void delFile(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
File myDelFile = new File(filePath);
myDelFile.delete();
} catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
}
}
/**
* 删除文件夹及文件夹下的所有内容
*
* @param folderPath
* 文件夹路径,如:c:/acd
*/
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); // 删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
myFilePath.delete(); // 删除空文件夹
} catch (Exception e) {
System.out.println("删除文件夹操作出错");
e.printStackTrace();
}
}
/**
* 删除文件夹里面的所有内容(包括文件夹、子文件夹、文件)
*
* @param path
* 文件夹路径
*/
public static void delAllFile(String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + File.separator + tempList[i]);// 先删除文件夹里面的文件
delFolder(path + File.separator + tempList[i]);// 再删除空文件夹
}
}
}
/**
* 移动文件到指定目录
*
* @param oldPath
* 原文件目录,如:c:/abc.txt
* @param newPath
* 移动后的文件目录,如:d:/acd/abc.txt
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
springmvc+mybatis+mysql+log4jdbc+excel 实现sql日志输出和excel导出 (104个子文件)
.classpath 1KB
org.eclipse.wst.common.component 676B
org.eclipse.wst.jsdt.ui.superType.container 49B
activity-style.css 8KB
app.css 654B
demo-rotate.html 3KB
shiro.ini 3KB
shiro.ini 3KB
FileUtil.java 9KB
TestController.java 7KB
ActivityController.java 5KB
Quickstart.java 5KB
Page.java 4KB
ViewExcel.java 3KB
AwardServlet.java 3KB
ExportReportController.java 3KB
RegexUtil.java 3KB
ExportExcelUtil.java 2KB
GoodsTest.java 2KB
UserController.java 2KB
RandomUtil.java 2KB
ExcelView.java 2KB
PropertiesUtil.java 2KB
ExcelUtil.java 2KB
GoodsInfo.java 2KB
RedisSpringTest.java 2KB
BaseDaoImpl.java 2KB
BigWheelDrawUtil.java 2KB
TestMybatis.java 2KB
Goods.java 1KB
FastJsonTools.java 1KB
User.java 1KB
RedisTest.java 1KB
BaseDao.java 972B
BaseDao.java 899B
WchatLotteryDomain.java 831B
BaseController.java 830B
Category.java 757B
ExcelTest.java 644B
CategoryDaoImpl.java 589B
UserDaoImpl.java 564B
Fruit.java 564B
ZSBCB2CPayTest.java 489B
UserServiceImpl.java 425B
ShiroTest.java 336B
UserService.java 127B
CategoryDao.java 110B
UserDao.java 100B
disk.jpg 45KB
jquery-1.7.2.min.js 93KB
jquery.min.js 88KB
jQueryRotate.2.2.js 11KB
jQueryRotate.2.2.js 11KB
jquery.easing.min.js 5KB
jquery.easing.min.js 5KB
.jsdtscope 575B
lottery.jsp 7KB
index.jsp 3KB
showUser.jsp 270B
org.eclipse.wst.jsdt.ui.superType.name 6B
ly-plate.png 309KB
activity-lottery-1.png 151KB
start.png 27KB
activity-lottery-2.png 18KB
rotate-static.png 16KB
1.png 5KB
org.eclipse.jdt.core.prefs 664B
org.eclipse.core.resources.prefs 155B
org.eclipse.m2e.core.prefs 90B
.project 1KB
log4j_2.properties 6KB
log4j_2.properties 6KB
log4j.properties 5KB
log4j.properties 5KB
log4j_1.properties 3KB
log4j_1.properties 3KB
log4j_jdbc.properties 3KB
log4j_jdbc.properties 3KB
config.properties 1KB
config.properties 1KB
goods.sql 78KB
goods.sql 78KB
exportReport.xls 21KB
template.xls 19KB
exportReport.xls 19KB
pom.xml 11KB
spring-shiro.xml 7KB
spring-shiro.xml 7KB
spring-mybatis.xml 6KB
spring-mybatis.xml 6KB
web.xml 3KB
spring-mvc.xml 2KB
spring-mvc.xml 2KB
spring-redis.xml 1KB
spring-redis.xml 1KB
mybatis-config.xml 1KB
mybatis-config.xml 1KB
UserMapper.xml 878B
UserMapper.xml 878B
root-bean.xml 777B
共 104 条
- 1
- 2
资源评论
- qq_371204952018-04-02还可以啦,好吧
竹林幽深
- 粉丝: 549
- 资源: 31
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功