File的util类

    这里集合了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);
 }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值