JSP file operations overview

Source: Internet
Author: User
Tags parent directory tomcat

1.File class

The object of the file class is used primarily to get some information about the file itself.

There are 3 construction methods for creating a File object:

File (String filename);

File (String directorypath,string filename);

File (file F, String filename);

FileName is the file name, DirectoryPath is the path to the file, and F is the file specified as a directory.

When you create a file by using file (String filename), the file is considered to be in the same directory as the current application, because the JSP engine starts executing under bin, so the

The file is considered to be in the following directory: D:\Tomcat\jakarta-tomcat-4.0\bin\.

1. Get the properties of a file

1. Public String getName (): Gets the name of the file.

2. public boolean CanRead (): Determines whether a file is readable.

3. public boolean canwrite (): Determines whether a file can be written to.

4. public Boolean exits (): Determines whether a file exists.

5. public long Length (): Gets the length of the file (in bytes).

6. Public String GetAbsolutePath (): Gets the absolute path to the file.

7. Public String GetParent (): Gets the parent directory of the file.

8. public boolean isfile (): Determines whether a file is a normal file, not a directory.

9. public boolean Isdirectroy (): Determines whether a file is a directory.

public boolean Ishidden (): Determines whether a file is a hidden file.

11. Public long LastModified (): Gets the time the file was last modified (the number of milliseconds from midnight 1970 to the last modified time of the file)

2. Create a table of contents

(1) Create a table of contents

File Object Invocation Method: Public boolean mkdir () creates a directory that returns false if the creation succeeds returns True (False if the directory already exists).

(2) Listing files in the directory

If the file object is a directory, the object can call the following methods to list the files and subdirectories under that directory:

Public string[] List (): Returns all files in a directory as a string,

public file [] Listfiles (): Returns all files in the directory as a file object.

(3) List files of the specified type

You can use the following two methods of the file class to list files of the specified type:

Public string[] List (FilenameFilter obj); This method returns all files of the specified type under the directory as a string.

Public File [] listfiles (FilenameFilter obj); This method returns all files of the specified type under the directory with the file object.

Filenamefile is an interface that has a method:

Public Boolean accept (File dir,string name);

When you pass an object that implements the interface to the list method, when the list method lists the file, it makes the file call the Accept method to check that the file conforms to the directory and file name requirements specified by the Accept method.

3. Delete Files and directories

The file object invocation method public Boolean Delete () can delete the files or directories represented by the current object, and if the file object represents a directory, the directory must be an empty directory, and the deletion succeeds to return true.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.