JSP file operation of additional text _jsp programming

Source: Internet
Author: User
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 to use two files, a JSP file, a JavaBean file, through the JSP call JavaBean can easily append data to the text file, if you read on the written article, you will find this article with a lot of similarities, read it is also very easy.
Note Place a text file Afile.txt to the Web root test directory so that the program can append the data, and JavaBean the class file to the corresponding class directory (Tomcat Environment) after the file is compiled.

writeappend.jsp

<title> Append Data </title>
<body bgcolor= "#000000" >
<%--Create JavaBean and set properties--%>
<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= "Initialize something Properties"/>
</jsp:useBean>


<p>
<%--sets the string to append--%>
<% writer.setsomething ("additional data"); %>
<%--reads the string set above--%>
<% Out.print (writer.getsomething ()); %>
<%--calls the writer Writesomething method to append the file and returns a success or error message--%>
<% Out.print (writer.writesomething ()); %>

</p>
</body>

Writeappend.java JavaBean File
Import java.io.*;

public class Writeappend {

Private String path;//file path
private string something;//appended string variable
Class
Public Writeappend () {
Path = NULL;
something = "Default message";
}
Set file path
public void SetPath (String apath) {
Path = Apath;
}
Get file path
Public String GetPath () {
return path;
}
Set the string to append
public void setsomething (String asomething) {
something = asomething;
}
Get the string to append
Public String getsomething () {
return something;
}
Append string
Public String writesomething () {
try {
Create a file path and write the something string, note the difference between the write and the article
FileWriter thefile = new FileWriter (path,true);
PrintWriter out = new PrintWriter (thefile);
Out.print (Something + "
");
Out.close ();
Closes the file and returns the success string
Thefile.close ();
return "success!!";
catch (IOException e) {
return e.tostring ();
}
}
}

OK, all the contents of this file operation are complete, if you see here, I believe you are OK for the basic operation of the file.
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.