package velcro.util;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* 对文本文件进行读写操作
*/
public class WriteAndReadText {
/**
* 文本文件所在的目录
*/
private String textPath;
/**
* 读取文本内容
* @param textname 文本名称
* @return
*/
public String readText(String textname){
File file=new File(textPath+File.separator+textname);
try {
BufferedReader br = new BufferedReader(new java.io.FileReader(file));
StringBuffer sb = new StringBuffer();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
br.close();
return sb.toString();
} catch (IOException e) {
LogInfo.error(this.getClass().getName(),e.getLocalizedMessage(),e);
e.printStackTrace();
return null;
}
}
}
/**
* 将内容写到文本中
* @param textname 文本名称
* @param date 写入的内容
* @return
*/
public boolean writeText(String textname,String date){
boolean flag=false;
File filePath=new File(textPath);
if(!filePath.exists()){
filePath.mkdirs();
}
try {
FileWriter fw =new FileWriter(textPath+File.separator+textname);
fw.write(date);
flag=true;
if(fw!=null)
fw.close();
} catch (IOException e) {
LogInfo.error(this.getClass().getName(),e.getMessage(),e);
e.printStackTrace();
}
return flag;
}
/**
* 在文档后附加内容
* @param textName
* @param date
* @return
*/
public boolean appendText(String textName,String date){
boolean flag=false;
File filePath=new File(textPath);
if(!filePath.exists()){
filePath.mkdirs();
}
try {
FileWriter fw =new FileWriter(textPath+File.separator+textName,true);
fw.append(date);
flag=true;
if(fw!=null)
fw.close();
} catch (IOException e) {
LogInfo.error(this.getClass().getName(),e.fillInStackTrace().toString());
e.printStackTrace();
}
return flag;
}
public String getTextPath() {
return textPath;
}
public void setTextPath(String textPath) {
this.textPath = textPath;
}
}
PrintWriter out = new PrintWriter(new FileWriter(logFileName, true), true);
Java读写文件最常用的类是FileInputStream/FileOutputStream和FileReader/FileWriter。
其中FileInputStream和FileOutputStream是基于字节流的,常用于读写二进制文件。
读写字符文件建议使用基于字符的FileReader和FileWriter,省去了字节与字符之间的转换。
但这两个类的构造函数默认使用系统的编码方式,如果文件内容与系统编码方式不一致,可能会出现乱码。
在这种情况下,建议使用FileReader和FileWriter的父类:InputStreamReader/OutputStreamWriter,
它们也是基于字符的,但在构造函数中可以指定编码类型:InputStreamReader(InputStream in, Charset cs) 和OutputStreamWriter(OutputStream out, Charset cs)。
// 读写文件的编码:
InputStreamReader r = new InputStreamReader(new FileInputStream(fileName), “utf-8″);
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileName),”utf-8″);
分享到:
相关推荐
java读取文件内容 转换成字符串 正则匹配获取所有手机号 并保存到另一文件中 文件可以是txt excel word 文件等。
java读取文件方法大全 读取file流等技术
java读取文件大全 写入字节流 读取字节流 在实际运用中相当的广泛 大家共享下资料
java读取文件内容java读取文件内容java读取文件内容
Java读写文件-Excel
Java 读取文件操作 java中多种方式读文件 一、多种方式读文件内容。 1、按字节读取文件内容 2、按字符读取文件内容 3、按行读取文件内容 4、随机读取文件内容
Java 读写文件文本文件的示例
Java读取文件方法大全Java读取文件方法大全Java读取文件方法大全Java读取文件方法大全
java读取TXT文件入库,java读取TXT文件入库,java读取TXT文件入库,
介绍Java直接读取、带缓冲读取、内存映射读取文件,并详细注释。
java读取文件内容的函数
java读写文件的方法,java操作文件
java读取文件中文乱码
自己写的一个最简单的 最易懂的 java读写文件的小例子 追加文件 回车换行等
\java读取文件方法大全.txt
java读写文件避免中文乱码.docx
java读写文件的方法.pdf
[Java]读取文件方法大全 1、按字节读取文件内容 2、按字符读取文件内容 3、按行读取文件内容 4、随机读取文件内容
1.java读取文件,输出文件 2.java正则表达式匹配* 2个文件内容是:中文 英文,然后分别比较中文部分和英文部分,文件内容如下: 520香烟 520cigarette 678月 678yue A-zone A-zone A.O.史密斯 AOsms ACE床垫 ACE ...