📄 fileutil.java
字号:
/**
*
*/
package hbu.david.cmc.util;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
/**
* @author Administrator
*
*/
public class FileUtil {
/**
*
*/
public FileUtil() {
// TODO Auto-generated constructor stub
}
/**
* 创建一个新的目录 如果创建成功返回这个新目录 创建失败就返回null
*
* @param file
* @return
*/
public static File createFileDir(File file) {
File f;
if (file.exists()) {
// 路径已经存在
f = file;
} else {
// 路径不存在
if (file.mkdirs()) {
// 如果创建成功
f = file;
} else {
// 创建失败
f = null;
}
}
return f;
}
/**
* 获取文件后缀名 并且改为小写
*/
public static String getFileSuffix(String fileName) {
String suffix;
suffix = fileName.trim().substring(fileName.trim().lastIndexOf(".") + 1)
.toLowerCase();
return suffix;
}
/**
* 改绝对路径为相对路径 注意是相对web应用名字的路径
*
* @param request
* @param path
* @return
*/
public static String changePathToRelative(HttpServletRequest request,
String path) {
String relativePath = "";
String contextPath = request.getContextPath();
// 获取该路径在Web中的路径
String webPath = path.substring(path.lastIndexOf(contextPath
.substring(1)));
// 从web路径中获取相对路径
relativePath = webPath.substring(request.getContextPath().length());
return relativePath;
}
/**
* 构造一个绝对路径
*/
public static File changePathToAbsol(HttpServletRequest request, String path){
return FileUtil.createFileDir(new File(request.getRealPath("")+changePath(path)));
}
/**
* 替换路径中的/和\
*/
public static String changePath(String path){
return path.replace("//", File.separator).replace("\\", File.separator);
}
/**
* 删除一个文件,如果是目录就删除目录和目录下的所有文件
*/
public static boolean deleteFile(File file){
boolean ok=true;
if(file.isFile()){
//文件
if(file.exists()){
file.delete();
}
}else if(file.isDirectory()){
//目录,递归删除
System.out.println(file.getName());
if(file.listFiles()==null||file.listFiles().length==0){
file.delete();
}else{
File[] f=file.listFiles();
for(int i=0;i<f.length;i++){
deleteFile(f[i]);
System.out.println(f[i].getName());
}
}
file.delete();
}
return ok;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("C://TEST//", "t.txt");
createFileDir(file);
System.out.print(deleteFile(file));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -