当前位置: 首页 > 图文教程 > 网络编程 > JSP > 使用JavaBean,一句代码完成对文本文件读取和写入!!!

JSP
我认为JSP有问题(上)
我认为JSP有问题(下)
jsp“抓”网页代码的程序
关于在bean里面打印html的利弊看法
bean里面如何打印到html页面
jdbc3中的RowSet 接口规范
Apusic Application Server1.0中jsp源代码泄漏漏洞
Unify的eWave ServletExec拒绝服务漏洞
通过提交超长的GET请求导致IBM HTTP Server远程溢出
在HTTP请求中添加特殊字符导致暴露JSP源代码文件
Resin 1.2 重要源代码暴露漏洞
多中WEB服务器的通用JSp源代码暴露漏洞
Tomcat 暴露JSP文件内容
IBM WebSphere Application Server 暴露JSP文件内容
JRun 2.3.x 范例文件暴露站点安全信息
BEA WebLogic 暴露源代码漏洞
IBM WebSphere Application Server 3.0.2 存在暴露源代码漏洞
Tomcat 3.1 存在暴露网站路径问题
Sun Java Web Server 能让攻击者远程执行任意命令
Netscape 修复 JAVA 安全漏洞

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-03   浏览: 184 ::
收藏到网摘: n/a

最近在做一个网站,需要对文本文件进行操作,本人为了方便,写了一个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文件
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*" %>
<html>
<head></head>
<body>
<jsp:useBean id="filecontrol" class="count.OP_File" scope="page"/>
<% 
 filecontrol.writefile("aa.txt","liuxiantong",false);
              //方法参数("路径","内容",true/false)--->是否追加
 String string=filecontrol.readfile("aa.txt");
              //方法:返回字符串 参数("路径")
 out.println(string);  //将读到的内容输出
%>
</body>
</html>