java 获取jspwriter_使用JavaBean,一句代码完成对文本文件读取和写入!!!

使用JavaBean,一句代码完成对文本文件读取和写入!!!

作者:    文章来源:

发布日期:2007年04月11日

最近在做一个网站,需要对文本文件进行操作,本人为了方便,写了一个JavaBean文本,

在jsp页面里,只需要两句代码就能够同时完成对文本文件的读取和写入.

JavaBean的代码如下......

package count;

import java.io.*;

public class OP_File

{

public BufferedReader bufread;

public BufferedWriter bufwriter;

File writefile;

String filepath,filecontent,read;

String readStr="";

public String readfile(String path)  //从文本文件中读取内容

{

try

{

filepath=path;                      //得到文本文件的路径

File file=new File(filepath);

FileReader fileread=new FileReader(file);

bufread=new BufferedReader(fileread);

while((read=bufread.readLine())!=null)

{

readStr=readStr+read;

}

}catch(Exception d){System.out.println(d.getMessage());}

return readStr;    //返回从文本文件中读取内容

}

//向文本文件中写入内容

public void writefile(String path,String content,boolean append)

{

try

{

boolean addStr=append; //通过这个对象来判断是否向文本文件中追加内容

filepath=path;       //得到文本文件的路径

filecontent=content; //需要写入的内容

writefile=new File(filepath);

if(writefile.exists()==false)    //如果文本文件不存在则创建它

{

writefile.createNewFile();

writefile=new File(filepath);  //重新实例化

}

FileWriter filewriter=new FileWriter(writefile,addStr);

bufwriter=new BufferedWriter(filewriter);

filewriter.write(filecontent);

filewriter.flush();

}catch(Exception d){System.out.println(d.getMessage());}

}

}

jsp文件

filecontrol.writefile("aa.txt","liuxiantong",false);

//方法参数("路径","内容",true/false)--->是否追加

String string=filecontrol.readfile("aa.txt");

//方法:返回字符串 参数("路径")

out.println(string);  //将读到的内容输出

%>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值