这里集合了file操作,包括,对文件属性的查询,file的新建,删除,移动(重命名)等。
陆续还会补充其他功能,也希望诸位网友提供新需求,MSN:changchun08521@hotmail.com
package test;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* <li>Title: FileUtil.java</li>
* <li>Project: server</li>
* <li>Package: java file operated</li>
* <li>Description: </li>
* <li>Copyright: Copyright (c) 2009</li>
* <li>Company: IdealTechnologies </li>
* <li>Created on May 9, 2009 1:21:39 PM</li>
* @author chun_chang
* @version 1.0
*/
public class FileUtil {
private static boolean isSuccess = false;
/**
* get file or folder base-info,include fileSize,occupySize,fileCount,folderCount,lastModefyTime
* @param file
* @param isNTFS true ntfs,false fat
* @return
* @CreateOn May 11, 2009 1:10:05 PM
* @author chun_chang
*/
public static Map<String, Long> getFileInfo(File file,boolean isNTFS) {
long fileSize = 0, occupySize = 0, fileCount = 0, folderCount = 0;
long lastModefyTime = file.lastModified();
Map<String, Long> map = new HashMap<String, Long>();
if (file.isDirectory()) {
folderCount++;
fileInfo(file);
fileCount = val[0];
fileSize = val[1];
} else {
fileCount = 1;
fileSize = file.length();
}
// (int(x/4096)+2)*4096 fat32 +2 ntfs +1
if(isNTFS){
occupySize = Math.round(((long) (Math.floor((fileSize) >>> 12) + 1) << 12));
}else{
occupySize = Math.round(((long) (Math.floor((fileSize) >>> 12) + 2) << 12));
}
map.put("fileSize", fileSize);
map.put("occupySize", occupySize);
map.put("fileCount", fileCount);
map.put("folderCount", folderCount);
map.put("lastModefyTime", lastModefyTime);
System.out.println(fileSize +"|"+ occupySize +"|"+ fileCount +"|"+ folderCount +"|"+ lastModefyTime);
return map;
}
/**
* 描述:getFileInfo 递归-计算文件夹下文件数和占用空间
*
* @param file
* @CreateOn Feb 28, 2009 1:33:13 PM
* @author chun_chang
*/
private static long[] val = new long[2];// fileCount fileSize
private static void fileInfo(File file) {
java.io.File[] f = file.listFiles();
for (int i = 0; i < f.length; i++) {
if (f[i].isDirectory()) {
fileInfo(f[i]);
} else {
val[0] += 1;
val[1] += f[i].length();
}
}
}
/**
* create file or folder.
* @param file
* @return
* @CreateOn May 9, 2009 1:27:04 PM
* @author chun_chang
*/
public static boolean createFile(File file) {
try {
if (file.isDirectory()) {
isSuccess = file.mkdirs();
} else {
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
isSuccess = file.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
return isSuccess;
}
/**
* delete file or folder.
* @param file
* @return
* @CreateOn May 9, 2009 1:30:06 PM
* @author chun_chang
*/
public static boolean deleteFile(File file) {
if (file.isDirectory()) {
File[] f = file.listFiles();
for (int i = 0; i < f.length; i++) {
isSuccess = deleteFile(f[i]);
}
}
isSuccess = file.delete();
return isSuccess;
}
/**
* move/copy file or folder.if <code>from</code> is a folder,move sub-files.
* @param from: from is file or folder
* @param to: to is folder
* @return
* @CreateOn May 9, 2009 1:45:45 PM
* @author chun_chang
*/
public static boolean fileMove(File from, File to) {
if (to.isDirectory()) {
if (from.isDirectory()) {
java.io.File[] files = from.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
java.io.File ff = new java.io.File(files[i].getPath());
java.io.File ft = new java.io.File(to + "/" + files[i].getName());
if (ft.exists()) {
FileUtil.deleteFile(ft);
} else {
ft.mkdirs();
}
fileMove(ff, ft);
} else {
java.io.File moveFile = new java.io.File(to.getPath() + "/" + files[i].getName());
if (moveFile.exists()) {
moveFile.delete();
}
isSuccess = files[i].renameTo(moveFile);
}
}
} else {
File ff = new File(new StringBuffer(from.getAbsolutePath() + "/" + to.getPath()).toString());
from.renameTo(ff);
}
} else {
System.out.println(" the dest path '" + to.getAbsolutePath() + "' must be a folder...");
}
return isSuccess;
}
/**
* @param args
* @CreateOn May 9, 2009 1:21:22 PM
* @author chun_chang
*/
public static void main(String[] args) {
File f1 = new File("D://Entegor//test.bat");
//createFile(f1);
getFileInfo(f1,false);
}
}