2008-06-20
文件读取
package cn.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.commons.lang.StringUtils;
public class FileUtils {
public static final String TEMPLATE_PATH = "c:\\FileUtils.java";
public static final String TEMPLATE_OUTPATH = "c:\\out.txt";
public static final String TEMPLATE_INPATH = "cn/test.txt";
public String getFileContent(String path,String charset){
StringBuffer sb=new StringBuffer();
String line;
BufferedReader br=null;
try {
br=new BufferedReader(new InputStreamReader(new FileInputStream(path),charset));
while((line=br.readLine())!=null){
sb.append(line);
sb.append("\n");
}
} catch (UnsupportedEncodingException e) {
System.out.println("读取文件有误!");
//e.printStackTrace();
} catch (IOException e) {
System.out.println("读取文件有误!");
//e.printStackTrace();
}finally{
if(!StringUtils.isEmpty(br.toString())){
try {
br.close();
} catch (IOException e) {
System.out.println("关闭文件有误");
e.printStackTrace();
}
}
}
return sb.toString();
}
public String fileReadToString(String path){
StringBuffer sb=new StringBuffer();
String line;
BufferedReader br=null;
try {
//ClassLoader.getSystemResourceAsStream(path)中的path路径是类路径下的文件
//如包cn下有test.txt文件,则路径是"cn/test.txt"。
br=new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream(path),"UTF-8"));
while((line=br.readLine())!=null){
sb.append(line);
sb.append("\n");
}
} catch (UnsupportedEncodingException e) {
System.out.println("读取文件有误!");
//e.printStackTrace();
} catch (IOException e) {
System.out.println("读取文件有误!");
//e.printStackTrace();
}finally{
if(!StringUtils.isEmpty(br.toString())){
try {
br.close();
} catch (IOException e) {
System.out.println("关闭文件有误");
e.printStackTrace();
}
}
}
return sb.toString();
}
public void stringWriteToTxt(String str,String path){
BufferedWriter bw=null;
try {
bw=new BufferedWriter(new FileWriter(path));
bw.write(str);
} catch (IOException e) {
System.out.println("写文件时有错误!");
e.printStackTrace();
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws FileNotFoundException{
FileUtils fileUtils=new FileUtils();
String str=fileUtils.fileReadToString(TEMPLATE_INPATH);
System.out.println(str);
fileUtils.stringWriteToTxt(str, TEMPLATE_OUTPATH);
File file=new File(TEMPLATE_OUTPATH);
FileInputStream is=new FileInputStream(file);
ClassLoader.getSystemResourceAsStream(TEMPLATE_INPATH);
}
}
发表评论
- 浏览: 8722 次
- 性别:

- 来自: 北京、四川

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
Struts2.0上传初步学习
...
-- by vtegymtd888 -
时间控件My97DatePicker
这种js控件的使用范围和语言无关。
-- by wutao8818 -
sqlserver2005数据库收缩 ...
...
-- by liveglobe -
Scrum 资料
恩。。。。。。。
-- by canofy -
Scrum 资料
感谢lz和大家分享好东西。不过附件还是去掉吧,让大家到InfoQ中文站上下载,多 ...
-- by JasonLai






评论排行榜