JSP读取文件的例子。

使用JSP读取文件示例
这是一个关于如何使用JSP从指定路径读取文件的示例。`ReadFile` Java Bean展示了如何打开、读取文件行并关闭文件。在JSP页面中,通过`jsp:useBean`和`jsp:setProperty`标签设置文件路径,然后循环读取并输出文件内容。

***************Bean代码ReadFile.java*******************************

_____________________________________________________________________

package temp;

import java.io.*;
import java.util.StringTokenizer;

public class ReadFile {
  private String currentRecord=null;
  private BufferedReader file;
  private String path;
  private StringTokenizer token;

  public ReadFile() {
    file=new BufferedReader(new InputStreamReader(System.in),1);
  }
  public ReadFile(String filePath) throws FileNotFoundException
  {
    path=filePath;
    file=new BufferedReader(new FileReader(path));
  }
 
  /**
   * 设置文件路径,并读取流
   * @param filePath String
   */
  public void setPath(String filePath)
  {
    path=filePath;
    try
    {
      file=new BufferedReader(new FileReader(path));
    }
    catch(FileNotFoundException e)
    {
      System.out.println("file not fount");
    }
  }
  /**
   * 返回路径
   * @return String
   */
  public String getPath()
  {
    return path;
  }

  /**
   *   关闭文件
   * @throws IOException
   */
  public void fileClose() throws IOException
  {
    file.close();
  }
  /**
   * 读取行
   * @return int
   */
  public int nextRecord()
  {
    int returnInt=-1;
    try
    {
      currentRecord=file.readLine();
    }
    catch(IOException e)
    {
      System.out.println("ReadLine Error");
    }

    if(currentRecord==null)
    {
      returnInt=-1;
    }
    else
    {
      token=new StringTokenizer(currentRecord);
      returnInt=token.countTokens();
    }
    return returnInt;
  }
  /**
   * 返回内容
   * @return String
   */
  public String returnRecord()
  {
    return currentRecord;
  }


}

***************************页面代码Read.jsp************

_______________________________________________________________________

<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
Read
</title>
</head>
<body bgcolor="#ffffff">
<jsp:useBean id="reader" scope="request" class="temp.ReadFile">
<jsp:setProperty name="reader" property="path" value="f://a.txt"/>
</jsp:useBean>
<h3>内容</h3>
<p></p>
<%
while (reader.nextRecord()!=-1)
{
 out.print(reader.returnRecord());
}
reader.fileClose();
%>
</body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值