java操作txt文件

java操作txt文件
没注意自己的blog竟然点击量突破4000了,自己的看起来发现竟然是自己写的各种java技术问题点击数量最大。好久没写点技术的问题了,加上最近也稍微做过一下java操作套文本文件的东西,那就把代码贴上来博取人气!!(*^__^*) 嘻嘻……

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值