|
Set thisArticleAdd to doube.com
File Creation/check and Deletion
<% @ 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
<% @ page contenttype = "text/html; charset = gb2312 "%>
<% @ page import =" Java. io. * "%>
Directory creation/check and deletion
<%
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 to it
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 ");
}< BR >%>
How to process virtual directories in JSP
<% @ 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
<% @ 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: <% = new date (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: <% = new date (F. lastmodified () %> <br>
<%
}
%>
</Body>
</Html>
method for extracting files from the directory
<% @ page contenttype = "text/html; charset = gb2312 "%>
<% @ page import =" Java. io. * "%>
how to retrieve files in a directory-list files in a directory
<%
string Path = request. getrealpath ("/");
file D = new file (PATH); // create a file object for the files in the current directory
file list [] = D. listfiles (); // get the array of file objects representing all files in the directory
out. println ("" + path + "files in the directory:
");
for (INT I = 0; I If (list [I]. isfile () {
out. println (list [I]. getname () + "
");
}< BR >}< br> out. println (" " + path + "Directory:
");
for (INT I = 0; I If (list [I]. isdirectory () {
out. println (list [I]. getname () + "
");
}< BR >%>
determine whether the file is a blank file
<% @ page contenttype = "text/html; charset = gb2312 "%>
<% @ page import =" Java. io. * "%>
identify Empty files
<%
string Path = request. getrealpath ("/");
out. println (PATH);
filereader Fr = new filereader (path + "\ atend.txt"); // create a filereader object, instantiate the object as Fr
// use the read () method for the object generated by the filereader class to read the next character from the character stream.
If (Fr. read () =-1) // judge whether the end Of the file has been read
{< br> out. print ("no data in the atend.txt file
");
}else {
out. println ("data in atend.txt file");
}< br> Fr. close ();
%>
Read all file data
<% @ page contenttype = "text/html; charset = gb2312" %>
<% @ page import = "Java. io. *, Java. lang. * "%>
Read all file data
<%
string Path = request. getrealpath (". ");
filereader Fr = new filereader (path +" \ readdata.txt ");
// The key lies in the reading process, to determine whether the read character has reached the end of the file and whether the character is a broken line character in the file, you can determine whether the character value is 13.
int c = Fr. Read (); // read one 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) {// determine whether it is a broken line character
out. print ("
"); // output branch label
Fr. skip (1); // skip one character
// C = Fr. read (); // read one character
}< BR >}< br> Fr. close ();
%>
reading data from one row and one row
<% @ page contenttype = "text/html; charset = gb2312 "%>
<% @ page import =" Java. io. * "%>
File Reading
<%
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 string to be read is not blank
while (line! = NULL) {
out. println (LINE + "
"); // output the data read from the file
line = BR. readline (); // read a row of data from the file
}< br> Br. close (); // close the bufferedreader object
Fr. close (); // close the file
%>
skipping characters in the file does not read
<% @ page contenttype = "text/html; charset = gb2312 "%>
<% @ page import =" Java. io. * "%>
skipped bytes are not read
<%
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 ();
}< br> Fr. close ();
%>
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. * "%>
data branches to write files
<%
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:
");
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 + "
");
line = BR. readline ();
}< br> Fr. close ();
%>
How to append data to a file
<% @ page contenttype = "text/html; charset = gb2312 "%>
<% @ page import =" Java. io. * "%>
data branches to write files
<%
string Path = request. getrealpath (". ");
randomaccessfile Rf = new randomaccessfile (path +" \ writedata.txt "," RW "); // defines an object of the randomaccessfile class, and instantiate
RF. seek (RF. length (); // move the pointer to the end of the file
RF. writebytes ("\ na Ppend a line to the file! ");
RF. close (); // close the file stream
out. println ("written file content:
");
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 ();
}< br> Fr. close (); // close the file
%>
|