jsp文件操作之写入篇(本站文章)
转载请注明来源
来源 jsp中国论坛 http://jspbbs.yeah.net
writeover.jsp
<html>
<head>
<title>write over a file</title>
</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>
import java.io.*;
/**
* writeover.java
* written by morgan catlin email: mfcatlin@csclub2.stthomas.edu
* august 19, 1999
*
* variables:
* string path = path to file to write to (ie. /you/afile.txt)
* string something = something to write to the file
*
* methods:
* void setpath() = sets path
* string getpath() = returns path
* void setsomething() = sets something
* string getsomething() = returns something
* string writesomething() = writes something to the path,
* returns a message that indicates success or failure(an exception)
*/
public class writeover {
private string path;
private string something;
public writeover() {
path = null;
something = "default message";
} // constructor writeover
/**
* mutator for the path property
*/
public void setpath(string apath) {
path = apath;
} // mutator setpath
/**
* accessor for the path property
*/
public string getpath() {
return path;
} // accessor getpathclient
/**
* mutator for the something property
*/
public void setsomething(string asomething) {
something = asomething;
} // mutator setsomething
/**
* accessor for the something property
*/
public string getsomething() {
return something;
} // accessor getsomething
/**
* this method writes something to the path
*/
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();
}
} // method writesomething
} // class writeover
转载请注明来源
来源 jsp中国论坛 http://jspbbs.yeah.net
