File establishment / check and delete <% @ page contentty = "text / html; charset = GB2312"%> <% @ page import = "java.io. *"%>
file Establish, check and delete title> 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 in 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,"); // Output is currently Directory path}%>
Contents of the catalog / check and deletion
<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "java.io. *"%>
directory creation / check and deletion Title> head> <% string path = request.getRealPath (""); path = path "// Sub"; // will be created directory path file d = new file (path); // Establish a File object representative of the SUB directory and get one reference IF (d.exists ()) {// check if the SUB directory exists (); out.println ("Sub directory, deleted"); } else {d.mkdir (); // Established subdirectory Out.println ("Sub directory does not exist, established");}%> body> html>
How to handle virtual directories in JSP
<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ Page Import = "Java.io. *"%>
JSP How to Handle Virtual Directory Title > head> Disk path to the virtual directory
WEB site main directory is
<% = request.getRealPath ("/")%> font> < BR> JSP page location directory location <% = request.getRealPath ("./")" g et / / <上 上 "j j j 位置 目 位置 上 位置 上 位置 位置 位置# ff0000> <% = Request.getRealPath ("../")" "" "" "
body> html> file properties
<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "java.util.date, java.io. *"%>
file properties Obtain Title> head> <% string path = request.getRealPath ("/"); file f = new file (path, "readdata.txt"); if (f.exists ()) { %> <% = f.getName ()%> following properties:
file length is:? <% = f.length ()%> <% = f.isFile () "is a file": "Not a file"%>
<% = f.Indirectory ()? "Is a directory": "Not a directory"%>
<% = f.canread ()? "Read": "Unreadable Take "%>
<% = f.canwrite ()?" Can be written ":" Not writable "%>
<% = f.ishidden ()?" Is a hidden file: "Not hidden File "%>
File's last modification date is: <% = new date (f.lastmodified ())%>
<%} else {f.createNewFile (); // Create one in the current directory a file named ReaData.txt%> <% = f.getName ()%> following properties:
file length is: <% = f.length ()%> <% = f.isFile ( "Yes File": "Not the file"%>
<% = f.Indirectory ()? "" Is a directory ":" Not a directory "%>
<% = f.canread ()?" Read ":" Do not read "%>
<% = f.canwrite ()?" Write ":" Not Write "%>
<% = f.ishidden ()?" Hidden file ":" Not hidden file "%>
File last modification date is: <% = new date (f.lastmodified ())%>
<%}%> body> html > Method for removing files in the directory
<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "java.io. *"%>
Remove the file in the directory - Column Files in the directory title> head> <% string path = request.getRealPath ("/"); file d = new file (path); // Establish the file object file in the current directory. File. List [] = d.listfiles (); // acquire file object array of all files on behalf of the directory Out.println ("" PATH "directory: font>
"); for (int i = 0; i " }}} Out.println ("
" "directory: font>
"); for (int i = 0; i ");}}%> body> html> read Take all file data
<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "java.io. *, java.lang. *"%>
Read all File Data Title> head> <% string path = request.getRealPath ("."); FileReader fr = new fileReader (Path "//ReadData.txt" );// The key lies in reading During the process, it is necessary to determine if 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 (); // reads a character // from the file to determine if the file ends are read, while (c! = - 1) {Out.print ((char) c); // Output Data c = fr.read (); // Continue to read data IF from the file (c == 13) {// Judgment whether it is a broken character out.print ("
"); // Output Branch Label fr.skip (1); // Skepts a character //c=fr.read();// read a character}} fr.close ();%> body> html>