您的位置:首页 > 其它

文件的简单读写操作,我有加注释!FileHelper

2009-06-02 13:44 369 查看
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public final class FileHelper {
// 标准 charset
public static final String US_ASCII = "US-ASCII";

public static final String ISO_8859_1 = "ISO-8859-1";

public static final String UTF8 = "UTF-8";

public static final String UTF16BE = "UTF-16BE";

public static final String UTF16LE = "UTF-16LE";

public static final String UTF16 = "UTF-16";
/*
* 为读取而打开文件
* @param path
* @param charset
* return BufferedReader
*
*/
public static BufferedReader openFileForRead(String path, String charset)
throws Exception {
//当path为null或空时,返回null
if (path == null || path.equals(""))
return null;
BufferedReader reader = null;
try {
if (charset != null && !charset.equals(""))
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(path), charset));
else
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(path)));
} catch (Exception e) {
throw e;
}
return reader;
}
/*
* 为读取而关闭文件
* @param reader
* @exception Exception
*
*/
public static void closeFileForRead(Object reader) throws Exception {
if (reader == null)
return;
if (reader instanceof BufferedReader) {
BufferedReader br = (BufferedReader) reader;
try {
br.close();
} catch (Exception e) {
throw e;
}
}
}
/*
* 读取文件
* @param path
* @param charset
* @exception Exception
* @return String
*/

public static String read(String path, String charset) throws Exception {
String str = "";
StringBuffer buf = new StringBuffer();
BufferedReader reader = null;
try {
reader = openFileForRead(path, charset);//调用静态方法openFileForRead

String line = reader.readLine(); //按行读取文件
while (line != null) {
buf.append(line).append("/n");
line = reader.readLine();
}
} catch (Exception e) {
throw e;
} finally {
closeFileForRead(reader);
}
str = buf.toString();
//去掉最后添加的"/n"
if(str != null && str.length() > 0){
str = str.substring(0, str.length() - 1);
}
return str;
}
/*
* 为写打开文件
* @param path
* @param charset
* @param append
* @exception Exception
* @return PrintWriter
*/
public static PrintWriter openFileForWrite(String path, String charset,
boolean append) throws Exception {
if (path == null || path.equals(""))
return null;
PrintWriter writer = null;
try {
if (charset == null || charset.equals("")) {
writer = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(path, append)));
} else {
writer = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(path, append), "UTF-8"));
}
} catch (Exception e) {
throw e;
}

return writer;
}
/*
* 写文件
* @param path String 目录
* @param charset String 字符
* @param append boolean 追加
* @param content String 内容
* @exception Exception
* @return void
*/

public static void write(String path, String content, String charset,
boolean append) throws Exception{
PrintWriter writer = null;
try {
writer = openFileForWrite(path, charset, append);
writer.write(content);
} catch (Exception e) {
throw e;
} finally {
closeFileForWrite(writer);
}
}
/*
* 为写文件而关闭文件
* @param writer Object PrintWriter的实例
* @exception Exception
* @return void
*/
public static void closeFileForWrite(Object writer) throws Exception{
if (writer == null)
return;
if (writer instanceof PrintWriter) {
PrintWriter pw = (PrintWriter) writer;
try {
pw.close();
} catch (Exception e) {
throw e;
}
}
}

/**
* 搜索指定目录下的文件和文件夹,找到filename后返回此文件的绝对路径
*
* @param dir
* 目标文件夹
* @param filename
* 目标文件名
* @param filetype
* 文件类型
* @param fullMatch
* 是否完全匹配文件名,true--文件名完全匹配;false--包含给定文件名即可
* @return
*/
public static String[] searchFile(String dir, String filename,
String filetype, boolean fullMatch) throws Exception {
String[] absolutePaths = null;
List<String> container = new ArrayList<String>();
File dirFile = new File(dir);
if (!dirFile.isDirectory()) {
throw new Exception("the argument:dir is not a directory!");
}
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
recurSearch(files[i], filename, filetype, fullMatch, container);
}
if (container != null) {
absolutePaths = new String[container.size()];
absolutePaths = (String[]) container.toArray(absolutePaths);
}
return absolutePaths;
}

/**
* 递归查找目录,找到符合的文件就把文件的绝对路径放入container 此方法为私有方法,由searchFile使用
*
* @param file
* @param filename
* @param filetype
* @param fullMatch
* @param container
*/
private static void recurSearch(File file, String filename,
String filetype, boolean fullMatch, List<String> container) {

if (file.isFile()) {
String name = file.getName();
if (fullMatch == true){
if(name.equalsIgnoreCase(filename + "." + filetype)) {
container.add(file.getAbsolutePath());
}
} else if (name.endsWith(filetype) && name.contains(filename)) {
container.add(file.getAbsolutePath());
}
} else {
File[] files = file.listFiles();
if(files != null){
for (int i = 0; i < files.length; i++)
recurSearch(files[i], filename, filetype, fullMatch, container);
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: