java操作文本文件个人认为最繁杂的是修改操作!需要重写整个文本!好了不说了先贴代码!
public class txtfile {
public void txtfileQ() throws IOException{ //txt文件的查询
FileReader fr =new FileReader("c://a.txt");
BufferedReader br = new BufferedReader(fr);
String Line=br.readLine();//从文件读取一行字符串
// 依据读取到的整行字符串是否不为空,来判断是够到达文件结尾
for (int i=0;Line !=null;i++){
if(Line.indexOf("测试")>-1){ // 使用indexof来实现模糊查询
System.out.println(Line+"1111111");
}
Line= br.readLine();
}
br.close();//关闭BufferedReader对象
fr.close();//关闭文件
}
//写txt文件 txtfile.getProperty("txtlist")为获得配置文件内的文件路径
public void txtfileW() throws IOException{
txtfile txtfile = new txtfile();
FileWriter fw=new FileWriter(txtfile.getProperty("txtlist"),true);//建立FileWriter对象,并实例化fw
// 将字符串写入文件
for (int i=0;i<20;i++){
fw.write("这是本文档的第"+i+"行"+"\r\n");
}
fw.write("测试耗费时间!");
fw.close();
}
//删除文件一行操作,可以类比为修改替换操作 txtfile.getProperty("newline")为配置文件内定义的回车换行符
public void txtfileD() throws IOException{
String line = "";
txtfile txtfile = new txtfile();
try {
File file = new File(txtfile.getProperty("txtlist"));
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存该行前面的内容
for (int j = 1; (line = br.readLine()) != null
&& !line.equals("这是本文档的第6行"); j++) {
buf = buf.append(line);
buf = buf.append(txtfile.getProperty("newline"));
}
// 将内容替换为空
buf = buf.append("");
// 保存该行后面的内容
while ((line = br.readLine()) != null) {
buf = buf.append(line);
buf = buf.append(txtfile.getProperty("newline"));
}
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//txt文件增加一行,追加到最后一行
public void txtfileA() throws IOException{
txtfile txtfile = new txtfile();
RandomAccessFile rf=new RandomAccessFile(txtfile.getProperty("txtlist"),"rw");
// 定义一个类RandomAccessFile的对象,并实例化。txtfile.getProperty("txtlist")为获得配置文件内的文件路径
rf.seek(rf.length());
rf.write("追加到文件最后一行!".getBytes());
rf.close();//关闭文件流
}
//读取配置文件获得文件路径
public String getProperty(String propertyname){
String propertyvalue = null;
Properties pro = new Properties ();
try {
FileInputStream fin = new FileInputStream("D://workspace//YNSMIMPL//WebRoot//WEB-INF//txt.properties");
pro.load(fin);
propertyvalue = pro.getProperty(propertyname);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return propertyvalue;
}
/*
* 遍历文件夹内文件并判断文件大小!
*/
public String SelectPath(String path){
File dir = new File(path);
File[] tempf=dir.listFiles();
String filenameN="";
if(dir.isDirectory()){
if(tempf.length>1){
for (int n=1;n<tempf.length;n++){
filenameN = path+"\\"+tempf[0].getName();
File a = new File(filenameN);
File b = new File(path+"\\"+tempf[n].getName());
if(a.length()>b.length()){
filenameN = path+"\\"+tempf[n].getName();
}else{
filenameN = path+"\\"+tempf[0].getName();
}
}
}else{
filenameN = path+"\\"+tempf[0].getName();
}
}
System.out.println(filenameN);
return filenameN;
}