ITeye博客

FileTool (文件工具类)

Listen_ing · 2011-03-07 16:51 · java综合 · 编程语言 · Access · Markdown

public class FileTool {

private static final Log log = LogFactory.getLog(FileTool.class);
private static final int BUFFER_SIZE = 16 * 1024 ;


/**
 * 删除目录和文件
 * 
 * @param fi
 * @throws IOException
 */
public static boolean deleteDirectory(File fi) throws IOException {
   if ((null != fi) && (fi.isDirectory() || fi.isFile())) {
       // 如果fi是文件直接删除
       if (fi.isFile()) {
          fi.delete();
          return true;
       } else { // 如果是目录,获取该目录下的所有文件和文件夹
          File files = fi.listFiles();
          int sz = files.length;
          for (int i = 0; i < sz; i++) {
              if (files*.isDirectory()) {
                 deleteDirectory(files*);
              } else {
                 files*.delete();
              }
          }
       }
       return true;
   } else {
       log.error("文件或目录删除失败", new IllegalArgumentException("异常信息: " + fi
              + " 不是一个目录或文件 "));
       throw new IllegalArgumentException("异常信息: " + fi + " 不是一个目录或文件 ");
   }
}


public static boolean deleteDirectoryAndParent(File fi) throws IOException {
   if ((null != fi) && (fi.isDirectory() || fi.isFile())) {
       // 如果fi是文件直接删除
       if (fi.isFile()) {
          fi.delete();
          return true;
       } else { // 如果是目录,获取该目录下的所有文件和文件夹
          File files = fi.listFiles();
          int sz = files.length;
          for (int i = 0; i < sz; i++) {
              if (files*.isDirectory()) {
                 deleteDirectoryAndParent(files*);
              } else {
                 files*.delete();
              }
          }
       }
       fi.delete();
       return true;
   } else {
       log.error("delete failed");
       throw new IllegalArgumentException("异常信息: " + fi + " 不是一个目录或文件 ");
   }
}
/**
 * 根据路径删除文件
 * @param filename
 * @return
 */
public static boolean deleteFile(String filename){
   File file = new File(filename);
   if(file.exists()){
       return file.delete();
   }
   return false;
}

/**
 * 创建目录
 * @param directoryName
 * @return boolean
 * @since Jan 6, 2010
 */
public static boolean createDirectory(String directoryName){
   File file=new File(directoryName);
   try {
       return file.mkdirs();
   } catch (Exception e) {
       log.error("create directory failed",e);
       return false;
   }
}

/**
 * 文件复制
 * @param src 源文件
 * @param dst 目标文件
 * @since Jan 21, 2010
 */
public static void copy(File src, File dst) {
   InputStream in = null;
   OutputStream out = null;
   try {
       in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
       out = new BufferedOutputStream(new FileOutputStream(dst),
              BUFFER_SIZE);
       byte buffer = new byte;
       int len = 0;
       while ((len = in.read(buffer)) > 0) {
          out.write(buffer, 0, len);
       }
   } catch (Exception e) {
       e.printStackTrace();
   } finally {
       if (null != in) {
          try {
              in.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
       }
       if (null != out) {
          try {
              out.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
       }
   }
}
/**
 * 根据多媒体文件格式,获取图片文件类型
 * @param mimeType 多媒体文件类型
 * @return String
 * @since Jan 21, 2010
 */
public static String getPicFileType(String mimeType){
   try {
       String typename = "";
       if (mimeType.equals("image/pjpeg")) {
          typename = ".jpg";
       } else if (mimeType.equals("image/bmp")) {
          typename = ".bmp";
       } else if (mimeType.equals("image/png")) {
          typename = ".png";
       } else if (mimeType.equals("image/gif")) {
          typename = ".gif";
       } else if (mimeType.equals("image/jpeg")) {
          typename = ".jpg";
       } else if (mimeType.equals("image/jpg")) {
          typename = ".jpg";
       } else if (mimeType.equals("image/x-png")) {
          typename = ".png";
       }
       return typename;
   } catch (Exception e) {
       log.error("get pic file type failed",e);
   }
   return null;
}

/**
 * 修改当前系统毫秒时间
 * @param fileType
 * @return String
 * @since Jan 22, 2010
 */
public static String getMTimeFileName(String fileType){
   try {
       Calendar cal=Calendar.getInstance();
       long mTime=cal.getTimeInMillis();
       return String.valueOf(mTime)+fileType;
   } catch (Exception e) {
       log.info("get Time In Millis to change the file name failed",e);
   }
   return null;
}

/**
 * 获取以长日期命名的文件名称
 * @param fileName
 * @return String
 * @since Jan 22, 2010
 */
@SuppressWarnings("static-access")
public static String getLongDataFileName(String fileType){
   try {
       Calendar calendar=Calendar.getInstance();
    StringBuffer fileName=new StringBuffer();
    fileName.append(calendar.get(calendar.YEAR));
    fileName.append(calendar.get(calendar.MONTH));
    fileName.append(calendar.get(calendar.DATE));
    fileName.append(calendar.get(calendar.HOUR_OF_DAY));
    fileName.append(calendar.get(calendar.MINUTE));
    fileName.append(calendar.get(calendar.SECOND));
    fileName.append(calendar.get(calendar.MILLISECOND));
    fileName.append(fileType);
    return fileName.toString();
   } catch (Exception e) {
       log.info("get long data to change file name failed",e);
   }
   return null;
}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。