天天看点

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;

   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.");

   }

   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());

   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;

   ret = (baos.tobytearray());

 public static boolean write(string content, string file) {

   return write(content, file, false);

   log.error("write(string,string)  file=" + file + "   ", e);

 public static boolean write(byte[] content, string file) {

  boolean ret = false;

  fileoutputstream fos = null;

   file filedir = new file(getpath(file));

   if (!filedir.exists())

    filedir.mkdirs();

   fos = new fileoutputstream(file);

   fos.write(content);

   ret = true;

   log.error("write(byte,string) file=" + file, e);

    if (fos != null)

     fos.close();

    log.error(e);

 public static boolean write(string content, string file, boolean append) {

   long t1 = system.currenttimemillis();

   if(append)

    fos = new fileoutputstream(file, append);

   else

    fos = new fileoutputstream(file);

   fos.write(content.getbytes());

   long t2 = system.currenttimemillis();

   log.error("write(string,string,boolean) file=" + file, e);

 public static string getpath(string f) {

   return f.substring(0, f.lastindexof("/"));

   return "./";

 public static string[] getfilelist(string dir) {

   file parent = new file(dir);

   if (!parent.isabsolute() || !parent.isdirectory()) {

    return null;

   return parent.list();

   return null;

 public static string[] getfilelist(final string dir, final string pattern) {

   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);

}