<% @ Page import = "java. io. *" %>
<Html>
<Head>
<Title> Lion interactive network = "example of reading and writing files in jsp (preferred for SUN Enterprise Applications) </title>
</Head>
<Body>
<%
// Write an object
String str = "WWW. LIONSKY. NET ";
String filename = request. getRealPath ("lionsky.txt ");
Java. io. File f = new java. io. File (filename );
If (! F. exists () // if the file is not saved, create
{
F. createNewFile ();
}
Try
{
PrintWriter pw = new PrintWriter (new FileOutputStream (filename ));
Pw. println (str); // write content
Pw. close ();
}
Catch (IOException e)
{
Out. println (e. getMessage ());
}
// Read the file
Java. io. FileReader fr = new java. io. FileReader (f );
Char [] buffer = new char [10];
Int length; // The number of characters read (a Chinese character is a character)
// Read the file content
Out. write (filename + "<br> ");
While (length = fr. read (buffer ))! =-1)
{
// Output
Out. write (buffer, 0, length );
}
Fr. close ();
%>
</Body>
</Html>
<% // Write the file String str = "WWW. LIONSKY. NET "; String filename = request. getRealPath ("lionsky.txt"); java. io. file f = new java. io. file (filename); if (! F. exists () // if the file is not saved, {f. createNewFile ();} try {PrintWriter pw = new PrintWriter (new FileOutputStream (filename); pw. println (str); // write content pw. close ();} catch (IOException e) {out. println (e. getMessage ();} // read the file java. io. fileReader fr = new java. io. fileReader (f); char [] buffer = new char [10]; int length; // number of characters read (one Chinese character is a character) // read the file content out. write (filename +"
"); While (length = fr. read (buffer ))! =-1) {// output out. write (buffer, 0, length) ;}fr. close (); %>