用户名: 密码: 验证码:           网站地图  高级搜索  RSS订阅  收藏本站
您的位置:主页 > 网络编程 > JSP >

在JSP中操作文件

[ 来源:Yesky | 作者:包路跃 | 更新日期:2008-3-15 19:01:37 | 人气: | 评论 0 条 ]
  综述:无论是用JavaServer Page(JSP)技术,还是ASP、PHP技术实现的网站,都可能有计数器、投票等功能,这些功能的实现离不开对文件的操作。由此可见,文件操作对网站的建设来说,有着很重要的作用。
  本章首先介绍了JSP中文件的基本操作,包括读取操作、写入操作以及追加操作,然后在此基础上,通过实例,说明如何通过这三种基本操作,来实现计数器、投票等复杂功能。
JSP对文件的基本操作有哪些?

  读取操作

  读取操作是文件操作的基本功能之一,在计数器、投票统计中有着广泛的应用。那么,该操作在JSP中是如何实现的呢?请看下面的例子。

  本例用到了两个文件,一个jsp文件,一个Javabean文件。通过jsp中调用Javabean可以轻松读取文本文件,注重请放置一个文本文件afile.txt到web根目录的test目录下,Javabean文件编译后将class文件放到对应的class目录下(tomcat环境)。

Read.jsp

<html>
<head>
<title>读取一个文件</title>

www.jc567.cn


</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
import Java.io.*;
import Java.util.StringTokenizer;

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

//创建文件对象 复制于hot007.com
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) hot007.com
     {
     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文件,一个Javabean文件。通过jsp中调用Javabean可以轻松读取文本文件,注重请放置一个文本文件afile.txt到web根目录的test目录下,Javabean文件编译后将class文件放到对应的class目录下(tomcat环境)。



WriteOver.jsp
<html>
<head>
<title>Write over a file</title> 复制于hot007.com
</head>
<body bgcolor="#000000">
<jsp:useBean id="writer" class="WriteOver" scope="request">
<jsp:setProperty name="writer" property="path" value="/path/to/afile.txt" />
<jsp:setProperty name="writer" property="something" value="Something already set as a property in WriteOver" />
</jsp:useBean>
<h3>Write to the file</h3>
<p>
<%
writer.setSomething("Something to write to the file");
out.print(writer.getSomething());
out.print(writer.writeSomething());
%>
</p>
</body>
</html>

WriteOver.Java
import Java.io.*;
public class WriteOver
{
private String path;
private String something;

// WriteOver构造器,用于初始化参数
public WriteOver() {
path = null;
something = "Default message"; 文章来源于www.jc567.cn
}

//设置文件路径
public void setPath(String apath) {
path = apath;
}

//获取路径参数
public String getPath() {
return path;
}

//获取something参数值
public void setSomething(String asomething) {
something = asomething;
}

//设置something参数
public String getSomething() {
return something;
}

//将something参数的值写入paht指定的文件中
public String writeSomething()
{
try {
     File f = new File(path);
     PrintWriter out = new PrintWriter(new FileWriter(f));
     out.print(this.getSomething() "\n");
     out.close();
return "Alles ist Gut.";
} catch (IOException e) {
     return e.toString();
}    
}
}

共4页: 上一页 1 [2] [3] [4] 下一页
Tags:在JSP中操作文件
您的评论
用户名: 新注册) 密码: 匿名评论 [所有评论]

·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为