File establishment / check and deletion
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Page Import = "java.io. *"%>
hEAD>
<%
String path = request.getRealPath ("");
//out.println (PATH);
FILE F = New File (Path, "File.txt");
//out.println (F);
//out.println (f.exists ());
IF (F.Exists ()) {// Check if file.txt exists
f.delete (); // Delete file.txt file
Out.println (Path "//file.txt exists, deleted.");
} else {
f.createNewFile (); // Create a file named file.txt in the current directory
Out.println (Path "//file.txt does not exist, established."); // Output current directory path
}
%>
Content / Check and Delete <% @ Page ContentType = "TEXT / HTML; Charset = GB2312"%>
<% @ Page Import = "java.io. *"%>
hEAD>
<%
String path = request.getRealPath ("");
Path = PATH "// Sub"; // will be created directory path
FILE D = New File (PATH); / / Establish a file object representing the SUB directory and gets a reference to it
IF (d.exists ()) {// check if the SUB directory exists
D. Delete ();
OUT.PRINTLN ("Sub directory, deleted");
} else {
D.mkdir (); // Establish a SUB directory
Out.println ("SUB directory does not exist, established");
}
%>
body>
html>
How to handle virtual directory <% @ Page ContentType = "text = GB2312"%> "in JSP
<% @ Page Import = "java.io. *"%>
hEAD>
Take a disk path corresponding to the virtual directory
The location of the main directory of the Web site is <% = request.getRealPath ("/")%> font>
JSP page location <% = request.getRealPath ("./")" g g / / / t t "j j 网 网 目 上 位置 上 位置 上 位置 上 位置" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" " > <% = Request.getRealPath ("..") "=" "
body>
html>
Document attribute
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Page Import = "java.util.date, java.io. *"%>
hEAD>
<%
String path = Request.getRealPath ("/");
File f = new file (path, "readdata.txt");
IF (f.exists ()) {
%>
<% = F.getName ()%> following properties:
The file length is: <% = f.length ()%>
<% = f.isfile ()? "Yes File": "Not File"%>
<% = f.IsDirectory ()? "is a directory": "Not a directory"%>
<% = f.canread ()? "Readable": "Do not read"%>
<% = f.canwrite ()? "writable": "Not Writing"%>
<% = f.ishidden ()? "is a hidden file": "Not hidden file"%>
The final modification date of the file is: <% = new Date (f.lastmodified ())%>
<%
} else {
f.createNewFile (); // Create a file called readata.txt in the current directory
%>
<% = F.getName ()%> following properties:
The file length is: <% = f.length ()%>
<% = f.isfile ()? "Yes File": "Not File"%>
<% = f.IsDirectory ()? "is a directory": "Not a directory"%>
<% = f.canread ()? "Readable": "Do not read"%>
<% = f.canwrite ()? "writable": "Not Writing"%>
<% = f.ishidden ()? "is a hidden file": "Not hidden file"%>
The final modification date of the file is: <% = new Date (f.lastmodified ())%>
<%}
%>
body>
html>
Take out the file in the directory <% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Page Import = "java.io. *"%>
hEAD>
<%
String path = Request.getRealPath ("/");
FILE D = New File (PATH); / / Establish the file object in the current directory
File List [] = D.ListFiles (); // A number of file objects that represent all files in the directory
Out.println ("" PATH "directory: font>
");
For (int I = 0; i IF (List .isfile ()) { Out.println (List .GetName () " } } Out.println (" For (int I = 0; i IF (List .Indirectory ()) { Out.println (List .GetName () " } } %> body> html> Judgment is a blank file <% @ Page ContentType = "text / html; charset = GB2312"%> <% @ Page Import = "java.io. *"%>
");
" "directory: font>
);
");
hEAD>
<%
String path = Request.getRealPath ("/");
Out.println (PATH);
FileReader fr = new fileReader (path "//atend.txt" );// establishes FileReader object, and instantiates FR
/ / The object generated by the FileReader class uses the read () method, which can read the next character from the character stream.
IF (fr.read () == - 1) // determine if the end of the file has been read
{
Out.print (no data
"in atend.txt file);
} else {
Out.println ("ATEND.TXT file has data");
}
Fr.close ();
%>
body>
html>
Read all file data
<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "java.io. *, java.lang. *"%>
hEAD>
<%
String path = Request.getRealPath (".");
FileReader fr = new fileReader (Path "//ReadData.txt");
/ / The key is that during the reading, it is to be determined whether the read characters have reached the end of the file, and this character is not a broken line in the file, ie it is determined whether the character value is 13.
INT C = fr.read (); // Read a character from the file
/ / Judgment whether the end of the file has been read
While (c! = - 1) {
OUT.PRINT ((char) c); / / Output read data
c = fr.read (); // Continue to read data from the file
IF (c == 13) {// Judgment whether it is a broken character
Out.print ("
"); // Output Branch Tag
Fr.skip (1); // Skose a character
//c=fr.read ();// read a character
}
}
Fr.close ();
%>
body>
html>
Read data a row
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Page Import = "java.io. *"%>
hEAD>
<%
String path = request.getRealPath (""); // get the path to the current directory
FileReader fr = new fileReader (Path "//file/inc///t.txt"" ;//> Establishing a FileReader object, and instantiate to FR
BufferedReader Br = New BufferedReader (fr); // Establish a bufferedReader object and instantiate to BR
String line = br.readline (); // read a string from the file
/ / Judgment whether the read string is not empty
While (line! = null) {
Out.println (Line "
"); / / Output data read from the file
LINE = br.readline (); // Continue to read a line of data from the file
}
Br.close (); // Close BufferedReader object
fr.close (); // Turk
%>
body>
html>
I'll not read the characters in the file
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Page Import = "java.io. *"%>
hEAD>
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 into files
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Page Import = "java.io. *"%>
hEAD>
<%
String path = Request.getRealPath (".");
FileWriter FW = New FileWriter (Path "//WriteData.txt": // Established a FileWriter object, and instantiate FW
// write a string to the file
fw.write ("Hello!");
Fw.write ("This book is" JSP Programming Skills "");
fw.write ("Please give me a lot!");
Fw.write ("email: stride@sina.com");
Fw.close ();
FileReader fr = new fileReader (Path "//writedata.txt");
BufferedReader Br = New BufferedReader (fr); // Establish a bufferedReader object and instantiate to BR
String line = br.readline ();
// read a line of data
Out.println (Line "
");
Br.close (); // Close BufferedReader object
Fr.close ();
%>
body>
html>
Write the data branch of the file
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Page Import = "java.io. *"%>
hEAD>
<%
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 (); // broken
BW.WRITE ("Please pay more attention!");
BW.NEWLINE (); // Broken BW.WRITE ("email: stride@sina.com");
Bw.flush (); // update data to file
fw.close (); // Turn off the file stream
Out.println ("Write file content is:
");
FileReader fr = new fileReader (Path "//writedata.txt");
BufferedReader Br = New BufferedReader (FR);
String line = br.readline (); // read a line of data
While (line! = null) {
Out.println (Line "
");
Line = br.readline ();
}
Fr.close ();
%>
body>
html>
How to add data to file <% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Page Import = "java.io. *"%>
hEAD>
<%
String path = Request.getRealPath (".");
RandomaccessFile Rf = new randomaccessfile (Path "//writedata.txt", "rw"), define a class randomaccessfile object, and instantiate
Rf.seek (rf.Length ()); // Move the pointer to the end of the file
Rf.writebytes ("/ NAPPEND A LINE to the File!");
Rf.close (); // Turn the file stream
Out.println ("Write file content is:
");
FileReader fr = new fileReader (Path "//writedata.txt");
BufferedReader Br = New BufferedReader (fr); // Read the bufferedRead object of the file
String line = br.readline ();
While (line! = null) {
Out.println (Line "
");
Line = br.readline ();
}
fr.close (); // Turk
%>
body>
html> i> i> i> i>