js| Server
Read data on one line
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> file Read </title>
<body>
<%
String Path=request.getrealpath ("");//Get the path to the current directory
FileReader fr=new FileReader (path + "\\file\\inc\\t.txt");//Create the FileReader object and instantiate it as FR
BufferedReader br=new BufferedReader (FR);//Establish BufferedReader object and instantiate as BR
String Line=br.readline ()//reading a line from a file
Determines whether the read string is not empty
while (Line!=null) {
Out.println (line + "<br>")//Output data read from file
Line=br.readline ()//Continue reading a row of data from a file
}
Br.close ()//Close BufferedReader Object
Fr.close ()//close file
%>
</body>
Skipping characters in a file does not read
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> Skip bytes Not read </title>
<body>
<%
String Path=request.getrealpath (".");
FileReader fr=new FileReader (path + "\\ReadData.txt");
Fr.skip (2);//Skip 2 bytes
int C=fr.read ()//read one byte
while (C!=-1) {
Out.print ((char) c);
C=fr.read ();
}
Fr.close ();
%>
</body>
Writing data to a file
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> Write data to file </title>
<body>
<%
String Path=request.getrealpath (".");
FileWriter fw=new FileWriter (path + "\\WriteData.txt");//Create FileWriter object and instantiate FW
Writing a string to a file
Fw.write ("Hello, everyone!") ");
Fw.write ("This book is the JSP Programming skill");
Fw.write ("Please advise!") ");
Fw.write ("email:stride@sina.com");
Fw.close ();
FileReader fr=new FileReader (path + "\\WriteData.txt");
BufferedReader br=new BufferedReader (FR);//Establish BufferedReader object and instantiate as BR
String Line=br.readline ();
Reading a row of data
Out.println (line + "<br>");
Br.close ()//Close BufferedReader Object
Fr.close ();
%>
</body>
The data branch that will be written to the file
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> data to be written to the file branch </title>
<body>
<%
String Path=request.getrealpath (".");
FileWriter fw=new FileWriter (path + "\\WriteData.txt");
BufferedWriter bw=new BufferedWriter (FW);
Bw.write ("Hello, everyone!") ");
Bw.write ("This book is the JSP Programming technique".) ");
Bw.newline ()/Break
Bw.write ("Please advise!") ");
Bw.newline ()/Break
Bw.write ("email:stride@sina.com");
Bw.flush ()//Update data to File
Fw.close ()//close file stream
Out.println ("Write file content is:<br>");
FileReader fr=new FileReader (path + "\\WriteData.txt");
BufferedReader br=new BufferedReader (FR);
String line=br.readline ();//reading a row of data
while (Line!=null) {
Out.println (line + "<br>");
Line=br.readline ();
}
Fr.close ();
%>
</body>
How to append data to a file
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> data to be written to the file branch </title>
<body>
<%
String Path=request.getrealpath (".");
Randomaccessfile rf=new randomaccessfile (path + "\\WriteData.txt", "RW");//define object for a class Randomaccessfile and instantiate
Rf.seek (Rf.length ());//move pointer to end of file
Rf.writebytes ("\nappend a line to the file!");
Rf.close ()//close file stream
Out.println ("Write file content is:<br>");
FileReader fr=new FileReader (path + "\\WriteData.txt");
BufferedReader br=new BufferedReader (FR);//Bufferedread object to read files
String Line=br.readline ();
while (Line!=null) {
Out.println (line + "<br>");
Line=br.readline ();
}
Fr.close ()//close file
%>
</body>