java中關于文件操作常用工具類

jopen 11年前發布 | 56K 次閱讀 Java開發 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; } }

}</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!