Read data in one row
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. Io. *" %>
<HTML>
<Head>
<Title> File Reading </title>
</Head>
<Body>
<%
String Path = request. getrealpath (""); // obtain the path of the current directory
Filereader Fr = new filereader (path + "// file // Inc // t.txt"); // create a filereader object and instantiate it as fr
Bufferedreader BR = new bufferedreader (FR); // create a bufferedreader object and instantiate it as Br
String line = Br. Readline (); // read a string from a file
// Judge whether the read string is not empty
While (line! = NULL ){
Out. println (LINE + "<br>"); // output the data read from the file.
Line = Br. Readline (); // continue reading a row of data from the file
}
BR. Close (); // close the bufferedreader object
Fr. Close (); // close the file
%>
</Body>
</Html>
Skipping characters in the file is not read
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. Io. *" %>
<HTML>
<Head>
<Title> skipped bytes are not read </title>
</Head>
<Body>
<%
String Path = request. getrealpath (".");
Filereader Fr = new filereader (path + "// readdata.txt ");
Fr. Skip (2); // skip 2 bytes
Int c = Fr. Read (); // read a byte
While (C! =-1 ){
Out. Print (char) C );
C = Fr. Read ();
}
Fr. Close ();
%>
</Body>
</Html>
Write data to a file
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. Io. *" %>
<HTML>
<Head>
<Title> write data to a file </title>
</Head>
<Body>
<%
String Path = request. getrealpath (".");
Filewriter fw = new filewriter (path + "// writedata.txt"); // create a filewriter object and instantiate the FW
// Write a string to a file
FW. Write ("Hello! ");
FW. Write ("This book is JSP programming skills");
FW. Write ("Please advise! ");
FW. Write ("Email: stride@sina.com ");
FW. Close ();
Filereader Fr = new filereader (path + "// writedata.txt ");
Bufferedreader BR = new bufferedreader (FR); // create a bufferedreader object and instantiate it as Br
String line = Br. Readline ();
// Read a row of data
Out. println (LINE + "<br> ");
BR. Close (); // close the bufferedreader object
Fr. Close ();
%>
</Body>
</Html>
Data branches that write files
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. Io. *" %>
<HTML>
<Head>
<Title> data branches that will write files </title>
</Head>
<Body>
<%
String Path = request. getrealpath (".");
Filewriter fw = new filewriter (path + "// writedata.txt ");
Bufferedwriter BW = new bufferedwriter (FW );
Bw. Write ("Hello! ");
Bw. Write ("This book is" JSP programming skills ". ");
Bw. newline (); // disconnected
Bw. Write ("Please advise! ");
Bw. newline (); // disconnected
Bw. Write ("Email: stride@sina.com ");
Bw. Flush (); // update data to a file
FW. Close (); // close the file stream
Out. println ("written file content: <br> ");
Filereader Fr = new filereader (path + "// writedata.txt ");
Bufferedreader BR = new bufferedreader (FR );
String line = Br. Readline (); // read a row of data
While (line! = NULL ){
Out. println (LINE + "<br> ");
Line = Br. Readline ();
}
Fr. Close ();
%>
</Body>
</Html>
How to append data to a file
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. Io. *" %>
<HTML>
<Head>
<Title> data branches that will write files </title>
</Head>
<Body>
<%
String Path = request. getrealpath (".");
Randomaccessfile Rf = new randomaccessfile (path + "// writedata.txt", "RW"); // defines an object of the randomaccessfile class and instantiate it.
RF. Seek (RF. Length (); // move the pointer to the end of the file
RF. writebytes ("/nappend a line to the file! ");
RF. Close (); // close the file stream
Out. println ("written file content: <br> ");
Filereader Fr = new filereader (path + "// writedata.txt ");
Bufferedreader BR = new bufferedreader (FR); // read the bufferedread object of the object
String line = Br. Readline ();
While (line! = NULL ){
Out. println (LINE + "<br> ");
Line = Br. Readline ();
}
Fr. Close (); // close the file
%>
</Body>
</Html> </I>