File operation is one of the important content of Web programming, ASP on file operation has been discussed a lot, let's see how JSP is implemented.
Here used two files, a JSP file a JavaBean file, through the JSP call JavaBean can easily write text files, note Please create a test directory to the Web root directory, The program will create a afile.txt file, JavaBean the file after compiling the class file into the corresponding class directory (Tomcat Environment).
There is a way to read and write files under the JSP, to make a simple counter to believe that it is not a difficult thing, we can try:
<p>
<%--sets the string to write--%>
<% writer.setsomething ("write something to a document"); %>
<%--reads the string set above--%>
<% Out.print (writer.getsomething ()); %>
<%--calls the writer Writesomething method to write to the file and returns a success or error message--%>
<% Out.print (writer.writesomething ()); %>
</p>
</body>
Writeover.java JavaBean File
Import java.io.*;
public class Writeover {
Private String path; File path
Strings written by private string something;//
Class
Public Writeover () {
Path = NULL;
something = "Default text";
}
Set file path
public void SetPath (String apath) {
Path = Apath;
}
Get file path
Public String GetPath () {
return path;
}
Get string
public void setsomething (String asomething) {
something = asomething;
}
Set string
Public String getsomething () {
return something;
}
Writes the string to the file, succeeds returns the success string
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 ();
}
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.