E-file:Cary


Cary的最新文章:
首页--文章内容

jsp文件操作
www.fh888.com 2006-1-4 17:48:42
媒体:网页天堂  作者:山无林
发布:Cary

写入:


 文件操作是网站编程的重要内容之一,asp关于文件操作讨论的已经很多了,让我们来看看jsp中是如何实现的。
  这里用到了两个文件,一个jsp文件一个javabean文件,通过jsp中调用javabean可以轻松写文本文件,注意请建立一个test目录到web根目录下,程序将会建立一个afile.txt文件,javabean文件编译后将class文件放到对应的class目录下(tomcat环境)。
  有了在jsp下读取和写入文件的方法,要做出一个简单的计数器来相信不是一件困难的事情了,大家可以尝试一下:)

WriteOver.Jsp

<html>
<head>
<title>写一个文件</title>
</head>
<body bgcolor="#000000">
<%--创建javabean并设置属性 --%>
<jsp:useBean id="writer" class="WriteOver" scope="request">
<jsp:setProperty name="writer" property="path" value="/test/afile.txt" />
<jsp:setProperty name="writer" property="something" value="初始化somthing属性" />
</jsp:useBean>

<h3>写一个文件</h3>

<p>
<%--设置要写入的字符串 --%>
<% writer.setSomething("写点东西到文件"); %>
<%--读取上面设置的字符串 --%>
<% out.print(writer.getSomething()); %>
<%--调用writer的writeSomething方法写入文件并返回成功或者出错信息 --%>
<% out.print(writer.writeSomething()); %>

</p>
</body>
</html>

//WriteOver.java javabean文件
import java.io.*;


public class WriteOver {

private String path; //文件路径
private String something;//写入的字符串
//初始化
public WriteOver() {
path = null;
something = "缺省文字";


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


//得到文件路径
public String getPath() {
return path;

//得到字符串
public void setSomething(String asomething) {
something = asomething;

//设置字符串
public String getSomething() {
return something;

//写入字符串到文件中,成功则返回success字符串
public String writeSomething() {
try {
    
     File f = new File(path);
     PrintWriter out = new PrintWriter(new FileWriter(f));
     out.print(this.getSomething() + "
");
     out.close();
     return "Success.";
} catch (IOException e) {
     return e.toString();
}    


 

追加:


文件操作是网站编程的重要内容之一,asp关于文件操作讨论的已经很多了,让我们来看看jsp中是如何实现的。
  这里用到了两个文件,一个jsp文件一个javabean文件,通过jsp中调用javabean可以轻松追加数据到文本文件,如果大家读了上写入篇的话,会发现这篇文章同上一篇有很多相似之处,读起来也很容易了。
注意请放置一个文本文件afile.txt到web根目录的test目录下,以便程序追加数据,javabean文件编译后将class文件放到对应的class目录下(tomcat环境)。

writeAppend.jsp

<html>
<head>
<title>追加数据</title>
</head>
<body bgcolor="#000000">
<%--创建javabean并设置属性 --%>
<jsp:useBean id="writer" class="WriteAppend" scope="request">
<jsp:setProperty name="writer" property="path" value="/path/to/afile.txt" />
<jsp:setProperty name="writer" property="something" value="初始化something属性" />
</jsp:useBean>

<h3>追加数据</h3>

<p>
<%--设置要追加的字符串 --%>
<% writer.setSomething("追加数据"); %>
<%--读取上面设置的字符串 --%>
<% out.print(writer.getSomething()); %>
<%--调用writer的writeSomething方法追加文件并返回成功或者出错信息 --%>
<% out.print(writer.writeSomething()); %>

</p>
</body>
</html>

//WriteAppend.java javabean文件
import java.io.*;

public class WriteAppend {

private String path;//文件路径
private String something;//追加的字符串变量
//初始化
public WriteAppend() {
path = null;
something = "Default message";

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

//得到文件路径
public String getPath() {
return path;

//设置要追加的字符串
public void setSomething(String asomething) {
something = asomething;

//得到要追加的字符串
public String getSomething() {
return something;

//追加字符串
public String writeSomething() {
try {
     //创建文件path并写入something字符串,注意和写入篇的区别
FileWriter theFile = new FileWriter(path,true);
PrintWriter out = new PrintWriter(theFile);
    out.print(something + "
");
    out.close();
//关闭文件并返回success字符串
    theFile.close();
    return "success!!";
} catch (IOException e) {
     return e.toString();
}    



  好了,到此文件操作的全部内容都完成了,如果您看到这里,相信您对文件基本操作已经OK了。

读取:



文件操作是网站编程的重要内容之一,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;


 
【声明】网页天堂刊载此文不代表同意其说法或描述,仅为提供更多信息,也不构成任何投资建议。转载请注明出处。
我也说两句
游客于2007-4-16 18:24:47写道:
qwerehgjkuil;iy
游客于2006-3-24 19:10:08写道:
s
E-File帐号:用户名: 密码: [注册]
评论:(内容不能超过500字,如果您不填写用户名和密码只能以游客的身份发表评论。)

*评论内容将在30分钟以后显示!
发表须知:
一、用户须严格遵守国家法律和政策,包括但不限于《全国人大常委会关于维护互联网安全的决定》《信息网络传播权保护条例》等规定,审慎、合法地利用伊妃(E-file)平台发表言论、作品。
二、用户的言论、行为若涉嫌违法或侵权,用户可能被强制承担因该行为直接或间接导致的全部法律责任。依照法律法规规定,伊妃(E-file)运营方有义务提供用户资料,有义务和权利采取删除、屏蔽、断开链接等各种必要措施。
三、伊妃(E-file)中心授权网络法律专业研究服务机构“网络法苑”为用户及客户提供包括免费咨询在内的全方位的法律支持。

 




Copyright (C) 2000-2006 fh888.com All Rights Reserved