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

被折叠的 条评论
为什么被折叠?



