File creation/check and deletion
The code is as follows: |
Copy code |
<% @ Page contentType = "text/html; charset = gb2312" %> <% @ Page import = "java. io. *" %> <Html> <Head> <Title> File creation, check, and deletion </title> </Head> <Body> <% 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 whether File.txt exists F. delete (); // delete the File.txt file Out. println (path + "\ File.txt exists and has been deleted. "); } Else { F. createNewFile (); // create a file named File.txt in the current directory. Out. println (path + "\ File.txt does not exist. It has been created. "); // Output the current directory path } %> |
Directory creation/check and deletion
The code is as follows: |
Copy code |
<% @ Page contentType = "text/html; charset = gb2312" %> <% @ Page import = "java. io. *" %> <Html> <Head> <Title> directory creation, check, and deletion </title> </Head> <Body> <% String path = request. getRealPath (""); Path = path + "\ Sub"; // directory path to be created File d = new File (path); // create a File object that represents the Sub directory and obtain a reference If (d. exists () {// check whether the Sub directory exists D. delete (); Out. println ("Sub directory exists, deleted "); } Else { D. mkdir (); // create a Sub directory Out. println ("Sub directory does not exist, created "); } %> </Body> </Html> |
How to process virtual directories in JSP
The code is as follows: |
Copy code |
<% @ Page contentType = "text/html; charset = gb2312" %> <% @ Page import = "java. io. *" %> <Html> <Head> <Title> How to handle virtual directories in JSP </title> </Head> <Body> Obtain the disk path corresponding to the virtual directory <br> <Font color = # ff0000> <% = request. getRealPath ("/") %> </font> <br> <Font color = # ff0000> <% = request. getRealPath ("./") %> </font> <br> <Font color = # ff0000> <% = request. getRealPath ("../") %> </font> <br> </Body> </Html> |
File attribute acquisition
The code is as follows: |
Copy code |
<% @ Page contentType = "text/html; charset = gb2312" %> <% @ Page import = "java. util. Date, java. io. *" %> <Html> <Head> <Title> File attribute acquisition </title> </Head> <Body> <% String path = request. getRealPath ("/"); File f = new File (path, "ReadData.txt "); If (f. exists ()){ %> The attributes of <% = f. getName () %> are as follows: <br> File length: <% = f. length () %> <% = F. isFile ()? "Yes file": "Not a file" %> <br> <% = F. isDirectory ()? "Yes directory": "not a directory" %> <br> <% = F. canRead ()? "Readable": "unreadable" %> <br> <% = F. canWrite ()? "Writable": "not writable" %> <br> <% = F. isHidden ()? "Hidden file": "Not hidden file" %> <br> The last modification date of the file is: <% = newDate (f. lastModified () %> <br> <% } Else { F. createNewFile (); // create a file named readata.txt in the current directory. %> The attributes of <% = f. getName () %> are as follows: <br> File length: <% = f. length () %> <% = F. isFile ()? "Yes file": "Not a file" %> <br> <% = F. isDirectory ()? "Yes directory": "not a directory" %> <br> <% = F. canRead ()? "Readable": "unreadable" %> <br> <% = F. canWrite ()? "Writable": "not writable" %> <br> <% = F. isHidden ()? "Hidden file": "Not hidden file" %> <br> The last modification date of the file is: <% = newDate (f. lastModified () %> <br> <% } %> </Body> </Html> |
How to retrieve files in a directory
The code is as follows: |
Copy code |
<% @ Page contentType = "text/html; charset = gb2312" %> <% @ Page import = "java. io. *" %> <Html> <Head> <Title> how to retrieve files in a directory-list files in a directory </title> </Head> <Body> <% String path = request. getRealPath ("/"); File d = new File (path); // create the File object of the File in the current directory File list [] = d. listFiles (); // retrieves an array of File objects representing all files in the directory Out. println ("<font color = # ff0000>" + path + "files in the directory: </font> <br> "); For (int I = 0; I <list. length; I ++ ){ If (list <I>. isFile ()){ Out. println (list <I>. getName () + "<br> "); } } Out. println ("<br> <font color = # ff0000>" + path + "directory: </font> <br> "); For (int I = 0; I <list. length; I ++ ){ If (list <I>. isDirectory ()){ Out. println (list <I>. getName () + "<br> "); } } %> </Body> </Html> |
Determines whether the file is blank.
The code is as follows: |
Copy code |
<% @ Page contentType = "text/html; charset = gb2312" %> <% @ Page import = "java. io. *" %> <Html> <Head> <Title> determine whether a blank file is used </title> </Head> <Body> <% String path = request. getRealPath ("/"); Out. println (path ); FileReader fr = new FileReader (path + "\ AtEnd.txt"); // Create a FileReader object and instantiate it as fr // Use the read () method for the object generated by the FileReader class to read the next character from the delimiter stream. If (fr. read () =-1) // determines whether the object has been read to the end. { Out. print ("there is no data in the AtEnd.txt file <br> "); } Else { Out. println ("data in the AtEnd.txt file "); } Fr. close (); %> </Body> </Html> |
Read all file data
The code is as follows: |
Copy code |
<% @ Page contentType = "text/html; charset = gb2312" %> <% @ Page import = "java. io. *, java. lang. *" %> <Html> <Head> <Title> READ all file data </title> </Head> <Body> <% String path = request. getRealPath ("."); FileReader fr = new FileReader (path + "\ ReadData.txt "); // The key lies in the reading process. It is necessary to determine whether the character has reached the end of the file and whether the character is a broken line character in the file, that is, to determine whether the character value is 13. Int c = fr. read (); // read a character from the file // Determine whether the object has been read to the end While (c! =-1 ){ Out. print (char) c); // output the read data C = fr. read (); // Continue reading data from the file If (c = 13) {// determines whether the row is a broken line character Out. print ("<br>"); // output branch label Fr. skip (1); // skip one character // C = fr. read (); // read a character } } Fr. close (); %> </Body> </Html> |
Read data in one row
The code is as follows: |
Copy code |
<% @ 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
The code is as follows: |
Copy code |
<% @ 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
The code is as follows: |
Copy code |
<% @ 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
The code is as follows: |
Copy code |
<% @ 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
The code is as follows: |
Copy code |
<% @ 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> |