java中关于文件操作常用工具类

jopen 11年前

import org.apache.commons.logging.Log;  import org.apache.commons.logging.LogFactory;    import java.io.*;  import java.util.regex.Pattern;    public class FileUtil {   private static final Log LOG = LogFactory.getLog(FileUtil.class);     public static boolean delete(String f) {    try {     File file = new File(f);     file.delete();     return true;    } catch (Exception e) {     LOG.error("delete(String) String f:" + "f", e);     return false;    }   }     public static String read(String file) {    String ret = null;      File f = null;    BufferedInputStream result = null;    ByteArrayOutputStream baos = null;      try {     f = new File(file);     if (!f.exists()) {      if (LOG.isDebugEnabled()) {       LOG.debug(file + " does not exist.");      }      return ret;     } else if (!f.isFile()) {      // fix bug:判断是否是文件,如果是一个目录是很危险的      LOG.warn(file + " is not a file.");      return ret;     }     result = new BufferedInputStream(new FileInputStream(f));     baos = new ByteArrayOutputStream();     byte[] cont = new byte[1024];     int conlen;     while ((conlen = result.read(cont)) >= 0) {      baos.write(cont, 0, conlen);     }     ret = new String(baos.toByteArray());    } catch (Exception e) {     LOG.error("read(String)  file:" + file, e);    } finally {     try {      if (result != null)       result.close();      if (baos != null)       baos.close();      f = null;     } catch (Exception e) {      LOG.error("read finally ", e);     }    }    return ret;   }   public static byte[] readBytes(String file) {    byte[] ret = null;      File f = null;    BufferedInputStream result = null;    ByteArrayOutputStream baos = null;      try {     f = new File(file);     if (!f.exists()) {      if (LOG.isDebugEnabled()) {       LOG.debug(file + " does not exist.");      }      return ret;     } else if (!f.isFile()) {      // fix bug:判断是否是文件,如果是一个目录是很危险的      LOG.warn(file + " is not a file.");      return ret;     }     result = new BufferedInputStream(new FileInputStream(f));     baos = new ByteArrayOutputStream();     byte[] cont = new byte[1024];     int conlen;     while ((conlen = result.read(cont)) >= 0) {      baos.write(cont, 0, conlen);     }     ret = (baos.toByteArray());    } catch (Exception e) {     LOG.error("read(String)  file:" + file, e);    } finally {     try {      if (result != null)       result.close();      if (baos != null)       baos.close();      f = null;     } catch (Exception e) {      LOG.error("read finally ", e);     }    }    return ret;   }     public static boolean write(String content, String file) {    try {     return write(content, file, false);    } catch (Exception e) {     LOG.error("write(String,String)  file=" + file + "   ", e);     return false;    }   }     public static boolean write(byte[] content, String file) {    boolean ret = false;      FileOutputStream fos = null;    try {     File filedir = new File(getPath(file));     if (!filedir.exists())      filedir.mkdirs();     fos = new FileOutputStream(file);     fos.write(content);     ret = true;    } catch (Exception e) {     LOG.error("write(byte,String) file=" + file, e);    } finally {     try {      if (fos != null)       fos.close();     } catch (Exception e) {      LOG.error(e);     }    }    return ret;   }     public static boolean write(String content, String file, boolean append) {    boolean ret = false;    FileOutputStream fos = null;      try {     long t1 = System.currentTimeMillis();     File filedir = new File(getPath(file));     if (!filedir.exists())      filedir.mkdirs();          if(append)      fos = new FileOutputStream(file, append);     else      fos = new FileOutputStream(file);          fos.write(content.getBytes());     long t2 = System.currentTimeMillis();     ret = true;    } catch (Exception e) {     LOG.error("write(String,String,boolean) file=" + file, e);     return false;    } finally {     try {      if (fos != null)       fos.close();     } catch (Exception e) {      LOG.error(e);     }    }    return ret;   }     public static String getPath(String f) {    try {     return f.substring(0, f.lastIndexOf("/"));    } catch (Exception e) {     return "./";    }   }     public static String[] getFileList(String dir) {    try {     File parent = new File(dir);     if (!parent.isAbsolute() || !parent.isDirectory()) {      return null;     }     return parent.list();    } catch (Exception e) {     return null;    }   }     public static String[] getFileList(final String dir, final String pattern) {    try {       File parent = new File(dir);     if (!parent.isAbsolute() || !parent.isDirectory()) {      return null;     }     final Pattern namePattern = Pattern.compile(pattern);     return parent.list(new FilenameFilter() {      public boolean accept(File dir, String name) {       if (namePattern.matcher(name).matches()) {        return true;       } else {        return false;       }      }     });      } catch (Throwable te) {     LOG.error("getFileList[dir=" + dir + ",pattern=" + pattern       + "]error.", te);     return null;    }   }    }