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

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 ;
   }
  }
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值