首页产品库评测行情新闻|手机数码笔记本台式机DIY硬件数字家庭数码相机办公外设|软件下载游戏开发|社区

更多

数码相机
MP4
LCD
机箱
音箱

天极网 > 软件频道>在JSP中操作文件

在JSP中操作文件

2002-04-23 17:37作者:出处:Yesky责任编辑:


  追加操作

  如何用jsp将数据追加到一个文件中呢?继续下面的例子吧。



writeAppend.jsp
<html>
<head>
<title>Append a file</title>
</head>
<body bgcolor="#000000">
<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 already set as a property in WriteAppend" />
</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>

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

// WriteAppend构造器,用于初始化参数
public WriteAppend() {
path = null;
something = "Default message";
}

//设置path参数
public void setPath(String apath) {
path = apath;
}

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

//设置参数something
public void setSomething(String asomething) {
something = asomething;
}

//获取something参数
public String getSomething() {
return something;
}

//追加操作
public String writeSomething()
{
try {
     FileWriter theFile = new FileWriter(path,true);
PrintWriter out = new PrintWriter(theFile);
     out.print(something + "\n");
     out.close();
    theFile.close();
     return "Das war sehr gut!";
} catch (IOException e) {
     return e.toString();
}    
}
}

  注意源程序中的加粗行,它是实现追加操作的关键。在创建FileWriter对象时,使用了FileWriter类的FileWriter(String filename , boolean append)构造函数,其中append参数用于决定对将写入的数据是否进行追加操作。
共6页。 9 1 2 3 4 5 6 :

关注此文的读者还看过:

返回软件频道首页

软件频道最新更新

热点推荐

天极服务|关于我们|About us|网站律师|RSS订阅|友情合作|加入我们|天极动态|网站地图|意见反馈|MSN/QQ上看天极
Copyright (C) 1999-2012 Yesky.com, All Rights Reserved 版权所有 天极网络