File operations are an important part of website programming. asp has discussed a lot about file operations. let's take a look at how jsp is implemented. Two files are used here. one jsp file and one javabean file can be easily appended to a text file by calling javabean in jsp. if you read the previous article, it will be found that this article has a lot of file operations as one of the important content of website programming, and asp has discussed a lot about file operations. let's take a look at how jsp is implemented.
Two files are used here. one jsp file and one javabean file can be easily appended to a text file by calling javabean in jsp. if you read the previous article, we will find that this article has many similarities with the previous article and it is easy to read.
Please put afile.txt in the directory test of the web root directory so that the program can append data. after compiling the javabean file, place the class file in the corresponding class directory (tomcat environment ).
WriteAppend. jsp
Append data
<% -- Create a javabean and set properties -- %>
Append data
<% -- Set the string to be appended -- %>
<% Writer. setSomething ("append data"); %>
<% -- Read the string set above -- %>
<% Out. print (writer. getSomething (); %>
<% -- Call writer's writeSomething method to append the file and return the success or error message -- %>
<% Out. print (writer. writeSomething (); %>
// WriteAppend. java javabean file
Import java. io .*;
Public class WriteAppend {
Private String path; // file path
Private String something; // Append String variable
// Initialization
Public WriteAppend (){
Path = null;
Something = "Default message ";
}
// Set the file path
Public void setPath (String apath ){
Path = apath;
}
// Obtain the file path
Public String getPath (){
Return path;
}
// Set the string to be appended
Public void setSomething (String asomething ){
Something = asomething;
}
// Obtain the string to be appended
Public String getSomething (){
Return something;
}
// Append the string
Public String writeSomething (){
Try {
// Create the file path and write the something string. Note the difference between the path and the writing.
FileWriter theFile = new FileWriter (path, true );
PrintWriter out = new PrintWriter (theFile );
Out. print (something +"
");
Out. close ();
// Close the file and return the success string
TheFile. close ();
Return "success !! ";
} Catch (IOException e ){
Return e. toString ();
}
}
}
All right, all the operations on this file have been completed. if you see this, I believe you are OK with the basic operations on the file.