package net.slqprrx.util.conf;
/**
* 作者:付星烨 E-mail:
* 创建时间:Aug 4, 2010 11:42:11 AM
* 类说明
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* @FileName:FileUtils.java
* @Description:文件操作的工具类
* @author: NQY
* @Version: 1.0
* @Createtime: 2008-12-12
*/
public class FileUtil {
/**
* 读取文本文件内容,以行的形式读取
*
* @param String
* filePathAndName 带有完整绝对路径的文件名
* @return String 返回文本文件的内容
*/
public static String readFileContent(String filePathAndName) throws IOException {
return readFileContent(filePathAndName,null,null,1024);
}
/**
* 读取文本文件内容,以行的形式读取
*
* @param String
* filePathAndName 带有完整绝对路径的文件名
* @param String
* encoding 文本文件打开的编码方式 例如 GBK,UTF-8
* @return String 返回文本文件的内容
*/
public static String readFileContent(String filePathAndName, String encoding) throws IOException {
return readFileContent(filePathAndName,encoding,null,1024);
}
/**
* 读取文本文件内容,以行的形式读取
*
* @param String
* filePathAndName 带有完整绝对路径的文件名
* @param int
* bufLen 设置缓冲区大小
* @return String 返回文本文件的内容
*/
public static String readFileContent(String filePathAndName,int bufLen) throws IOException {
return readFileContent(filePathAndName,null,null,bufLen);
}
/**
* 读取文本文件内容,以行的形式读取
*
* @param String
* filePathAndName 带有完整绝对路径的文件名
* @param String
* encoding 文本文件打开的编码方式 例如 GBK,UTF-8
* @param String
* sep 分隔符 例如:#,默认为空格
* @return String 返回文本文件的内容
*/
public static String readFileContent(String filePathAndName, String encoding, String sep) throws IOException {
return readFileContent(filePathAndName,encoding,sep,1024);
}
/**
* 读取文本文件内容,以行的形式读取
*
* @param String
* filePathAndName 带有完整绝对路径的文件名
* @param String
* encoding 文本文件打开的编码方式 例如 GBK,UTF-8
* @param String
* sep 分隔符 例如:#,默认为\n;
* @param int
* bufLen 设置缓冲区大小
* @return String 返回文本文件的内容
*/
public static String readFileContent(String filePathAndName, String encoding, String sep,int bufLen ) throws IOException {
if (filePathAndName == null || filePathAndName.equals("")) {
return "";
}
if(sep==null||sep.equals("")) {
sep="\n";
}
if(!new File(filePathAndName).exists()) {
return "";
}
StringBuffer str = new StringBuffer("");
FileInputStream fs = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
fs = new FileInputStream(filePathAndName);
if (encoding == null||encoding.trim().equals("")) {
isr = new InputStreamReader(fs);
} else {
isr = new InputStreamReader(fs, encoding.trim());
}
br = new BufferedReader(isr,bufLen);
String data = "";
while ((data = br.readLine()) != null) {
str.append(data).append(sep);
}
} catch (IOException e) {
throw e;
} finally {
try {
if(br != null)
br.close();
if(isr != null)
isr.close();
if(fs != null)
fs.close();
} catch(IOException e) {
throw e;
}
}
return str.toString();
}
/**
* 新建一个文件并写入内容
*
* @param String
* path 文件全路径
* @param String
* fileName 文件名
* @param StringBuffer
* content 内容
* @return boolean
* @throws IOException
*/
public static boolean newFile(String path, String fileName, StringBuffer content) throws IOException {
return newFile(path, fileName, content, 1024, false);
}
/**
* 新建一个文件并写入内容
*
* @param String
* path 文件全路径
* @param String
* fileName 文件名
* @param StringBuffer
* content 内容
* @return boolean
* @throws IOException
*/
public static boolean newFile(String path, String fileName, StringBuffer content, boolean isWrite) throws IOException {
return newFile(path, fileName, content, 1024, isWrite);
}
/**
* 新建一个文件并写入内容
*
* @param String
* path 文件全路径
* @param String
* fileName 文件名
* @param StringBuffer
* content 内容
* @param int
* bufLen 设置缓冲区大小
* @param boolean
* isWrite 是否追加写入文件
* @return boolean
* @throws IOException
*/
public static boolean newFile(String path, String fileName, StringBuffer content,
int bufLen, boolean isWrite) throws IOException {
if (path == null || path.equals("") || content == null || content.equals(""))
return false;
boolean flag = false;
FileWriter fw = null;
BufferedWriter bw = null;
File file = new File(path);
if(!file.exists()) {
file.mkdirs();
}
try {
fw = new FileWriter(path +File.separator+ fileName, isWrite);
bw = new BufferedWriter(fw, bufLen);
bw.write(content.toString());
flag = true;
} catch (IOException e) {
System.out.println("写入文件出错");
flag = false;
throw e;
} finally {
if (bw != null) {
bw.flush();
bw.close();
}
if (fw != null)
fw.close();
}
return flag;
}
/**
* 新建一个文件并写入内容
*
* @param String
* path 文件全路径
* @param String
* fileName 文件名
* @param String
* content 内容
* @return boolean
* @throws IOException
*/
public static boolean newFile(String path, String fileName, String content) throws IOException {
return newFile(path, fileName, content, 1024, false);
}
/**
* 新建一个文件并写入内容
*
* @param String
* path 文件全路径
* @param String
* fileName 文件名
* @param String
* content 内容
* @param boolean
* isWrite 是否追加写入文件
* @return boolean
* @throws IOException
*/
public static boolean newFile(String path, String fileName, String content, boolean isWrite) throws IOException {
return newFile(path, fileName, content, 1024, isWrite);
}
/**
* 新建一个文件并写入内容
*
* @param String
* path 文件全路径
* @param String
* fileName 文件名
* @param String
* content 内容
* @param int
* bufLen 设置缓冲区大小
* @param boolean
* isWrite 是否追加写入文件
* @return boolean
* @throws IOException
*/
public static boolean newFile(String path, String fileName, String content, int bufLen, boolean isWrite) throws IOException {
if (path == null || path.equals("") || content == null || content.equals(""))
return false;
boolean flag = false;
FileWriter fw = null;
BufferedWriter bw = null;
File file = new
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
conf_util.zip (6个子文件)
config.properties 264B
conf
Config.java 1KB
FileUtil.java 18KB
ConfReader.java 1KB
conf_util.jar 13KB
使用说明.java 518B
共 6 条
- 1
资源评论
- coffeefriend212013-08-17很好,已经拿来用了啊,多谢解决我来比较急的问题,
山河一梦
- 粉丝: 0
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功