快乐下载 - 软件下载 - 游戏下载 - 破解补丁 - 下载排行 - 软件分类 - 最近更新 - 发布软件
资讯 | 火热新闻 | 网络冲浪 | 操作系统 | 精文荟萃 | 健康知识 | 合同书类 | 笔记本类 | 办公软件 | 专题栏目 | 设计学院 | 游戏技巧 | ASP技术 | PHP技术 | JSP技术
首页 | 网络工具 | 系统工具 | 多媒体类 | 图形图像 | 应用软件 | 行业软件 | 教育教学 | 安全相关 | 游戏娱乐 | 源码下载 | 电子书库 | 编程开发
您的位置:首页 -> 新闻中心 -> .net技术-> .net-> jsp文件操作之读取篇

jsp文件操作之读取篇


发布时间:2005-2-22 人气:379 软件下载 资讯


2000-11-28· coolknight·yesky

文件操作是网站编程的重要内容之一,asp关于文件操作讨论的已经很多了,让我们来看看jsp中是如何实现的。
这里用到了两个文件,一个jsp文件一个javabean文件,通过jsp中调用javabean可以轻松读取文本文件,注意请放置一个文本文件afile.txt到web根目录的test目录下,javabean文件编译后将class文件放到对应的class目录下(tomcat环境)。
Read.jsp

<html>
<head>
<title>读取一个文件</title>
</head>
<body bgcolor="#000000">
<%--调用javabean --%>
<jsp:useBean id="reader" class="DelimitedDataFile" scope="request">
<jsp:setProperty name="reader" property="path" value="/test/afile.txt" />
</jsp:useBean>

<h3>文件内容:</h3>

<p>

<% int count = 0; %>
<% while (reader.nextRecord() != -1) { %>
<% count++; %>
<b>第<% out.print(count); %>行:</b>
<% out.print(reader.returnRecord()); %><br>
<% } %>
</p>
</body>
</html>


//DelimitedDataFile.java bean文件源代码
//导入java包
import java.io.*;
import java.util.StringTokenizer;

public class DelimitedDataFile
{

private String currentRecord = null;
private BufferedReader file;
private String path;
private StringTokenizer token;
//创建文件对象
public DelimitedDataFile()
{
file = new BufferedReader(new InputStreamReader(System.in),1);
}
public DelimitedDataFile(String filePath) throws FileNotFoundException
{

path = filePath;
file = new BufferedReader(new FileReader(path));
}
//设置文件路径
public void setPath(String filePath)
{

path = filePath;
try {
file = new BufferedReader(new
FileReader(path));
} catch (FileNotFoundException e) {
System.out.println("file not found");
}

}
//得到文件路径
public String getPath() {
return path;
}
//关闭文件
public void fileClose() throws IOException
{

file.close();
}
//读取下一行记录,若没有则返回-1
public int nextRecord()
{


int returnInt = -1;
try
{
currentRecord = file.readLine();
}

catch (IOException e)
{
System.out.println("readLine problem, terminating.");
}

if (currentRecord == null)
returnInt = -1;
else
{
token = new StringTokenizer(currentRecord);
returnInt = token.countTokens();
}
return returnInt;
}

//以字符串的形式返回整个记录
public String returnRecord()
{

return currentRecord;
}
}


为了对文件操作有全面了解,请看下一篇<<jsp文件操作之写入篇>>。

(出处:)



相关文章


相关软件

热门文章
本站首页 | 软件分类 | 资讯中心 | 网站地图 | 发布软件