Document directory
- HTML tags and JavaScript tutorial
HTML tags and JavaScript tutorial
Operations on files in JSP
JSP
File Operations
JSP
File operations are very important. To describe the attributes of a file, you must use
File
Class, its method does not involve reading and writing files, but it is very important to describe the files.
1.
Create
File
Object
File (string filename)
Create a file object with a file name
File (string directpath, string filename)
Create an object using the path and file name of the file storage
File (file F, string filename)
Create an object with a file object and a file name
NOTE: If no path or directory is specified, the new file is located in the default directory of the server.
2.
Create directory
Public Boolean mkdir ()
3.
List Files And Directories
Public String [] list ()
List Files And Directories In String Array Mode
Public file [] list ()
To
File
Object method to list files and directories
4.
List objects and directories with specified conditions
Public String [] list (filenamefilter OBJ)
Public String [] list (filenamefilter OBJ)
Filenamefilter
Is an interface, which has
Accept
Method
Public Boolean accept (flie Dir, string name)
In a specific application: when listing a specified type, you must declare a class to implement
Filenamefilter
Interface
Accept
Method. In the following program film, first instantiate
File
Object, specify the directory, instantiate the class that implements the interface, and take this as a parameter to form
Filenamefilter
Object, followed by calling
List
Or
Listfile
Method To list specified files.
Program example:
<% @ Page Language = "Java" %>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page errorpage = "" %>
<% @ Page import = "Java. Io. *" %>
<! Doctype HTML public "-// W 3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> file filter </title>
</Head>
<Body bgcolor = "# ffffff">
<%! Class filefilterjsp implements filenamefilter
{String STR = NULL;
Filefilterjsp (string S)
{STR = "." + S ;}
Public Boolean accept (File Dir, string name)
{Return name. endswith (STR );}//
Statement
Filefilterjsp
Class implementation
Filenamefilter
Interface
Accept
Method
} %>
<% File dir = new file ("G :/
Personal Materials
/
Design
");
Filefilterjsp file_jsp = new filefilterjsp ("jpg ");//
Instantiation
Filefilterjsp
Class, indicating the suffix of the file
String file_name [] = dir. List (file_jsp );//
By instantiating
File_jsp
List required file names
For (INT I = 0; I <file_name.length; I ++)
{Out. Print ("<br>" + file_name [I]);}
%>
</Body>
</Html>