java中关于文件操作常用工具类
来源:互联网 发布:输出php配置信息的函数 编辑:程序博客网 时间:2024/05/11 19:40
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;
}
}
}
- java中关于文件操作常用工具类
- java中关于文件操作常用工具类
- java中操作数组的常用工具类
- Java中常用工具类
- Java读取文件常用工具类
- java中常用工具类【1】
- Java中常用工具类(效率)
- java中并发常用工具类
- Java数组操作的常用工具类
- java日期操作常用工具
- [Java工具类]Spring常用工具类 1.文件资源操作和Web相关工具类
- Java常用工具类五 -- 文件处理相关
- Java String,MD5,文件 常用工具类
- java常用工具文件简介
- Java工作中常用工具类总结一
- Java关于File文件类操作总结
- Java常用工具类封装——String操作工具类
- Java常用工具类封装——String操作工具类
- xcode 快捷键
- Android 唯一识别码
- C99标准变化
- Android2.0 如何设置和读取联系人的分组(Group)
- Let the light guide us HDU
- java中关于文件操作常用工具类
- hdu_1166 敌兵布阵(树状数组)
- Linux下如何用GCC编译动态库
- 新手该如何学python怎么学好python
- JSF+AJAX企业级开发之路
- struts2之optiontransferselect标签(从jsp跑通到action)(详解)
- POJ 3321 Apple Tree(DFS+树状数组)
- java 文件建立删除
- 安装Zimbra