Java Foundation-file Class

Source: Internet
Author: User

The first file class

An overview

1. File class: Abstract representation of path names for files and directories

2. Features and functions:

1) used to encapsulate a file or folder as an object

2) Easy to manipulate the file and folder property information

3) An instance of the file class is immutable; that is, once created, the abstract path name represented by the file object will never change

4) The file object can be passed as a parameter to the constructor of the stream

Building two File objects

1. File f =new file (String pathname)    

Get the file object based on a path

2. File f =new file (string parent, String child)

Get the file object based on a directory and a sub-file/directory

3. File f =new file (file parent,string child)

Get the file object based on a parent file object and a sub-file/directory

Common functions of three file classes

A. Creating features

public boolean createnewfile (): Create file If such a file exists, it is not created and returns false

public boolean mkdir (): Create folder if such a folder exists, it is not created and returns false

public boolean mkdirs (): Create a folder, if the parent folder does not exist, will help you to create and return False

    Note: If you create a file or folder where you forgot to write the drive path, the default is under Project path.

B. Renaming and deleting features

public boolean Renameto (File dest): File1.renameto (File2)

If the File1 and File2 paths are the same, change the File1 file name to the File2 file name. If the path is different, cut

public boolean Delete (): Deletes the file or folder, returning whether the operation was successful

      Note: To delete a folder, please note that the folder cannot contain files or folders, otherwise it cannot be successfully manipulated.

and delete function does not go to the Recycle Bin, directly lost

C. Judging function

* Public boolean isdirectory (): Determine if it is a directory

* Public boolean isfile (): Determines whether the file is

* Public Boolean exists (): Determine if there is

* Public boolean CanRead (): Determines whether it is readable, Windows considers all files readable

* Public boolean CanWrite (): Determines if writable

Windows can be set to unreadable by File.setwriteable (false), then the file cannot be written by IO stream

* Public boolean Ishidden (): Determines whether to hide

* Boolean Isabsolute (): Determine if the file is an absolute path

D. Get Features

* Public String GetAbsolutePath (): Gets the absolute path

* Public String GetPath (): Gets the path in the constructor method

* Public String GetName (): Gets the file name and returns null if it is a directory

* Public long Length (): Gets the length. Number of bytes

* Public String getParent (): Get parent directory

* Public Long LastModified (): Gets the last modified time, millisecond value

    * Public string[] List (): Gets an array of names for all files or folders under the specified directory

* Public file[] Listfiles (): Gets the file array of all files or folders under the specified directory

Four lists file and file filtering

String[] List () Get all Files

String[]list (filenamefilter Filter): Returns a string array that gets the file or directory in the directory that satisfies the specified filter

File[] Listfiles (): Returns an array of file objects, getting all the files and folders under the current folder

  File[] Listfiles (filenamefilterfilter): Returns an array of file objects, gets the files or directories in the directory that satisfy the specified filter

Example 1: Determine if there is a TXT file under f disk, if there is a direct output
Method 1: Use File.listfiles () to get all the file objects, loop judgment

New File ("f:/"); file[] subfiles= f.listfiles ();  for (File subfile:subfiles) {     if(Subfile.isfile () &&subfile.getname (). EndsWith (". txt" ) {          System.out.println (subfile);     }}

    Method 2: Use file[] Listfiles (filenamefilterfilter), add filter to determine

New File ("f:/"); file[] Subfiles= f.listfiles (new  filenamefilter () {    @Override    public  Boolean  Accept (file dir, String name) {        new  file (dir, name);         return file.isfile () &&file.getname (). EndsWith (". txt");}    );  for (File subfile:subfiles) {    System.out.println (subfile);}

However, one problem is that you can only tell if the TXT file is included in the directory, and if it contains another directory

You cannot tell if the TXT file is included in the other directory, so we need to use the loop

Example upgrade: Determine if the directory contains a TXT file, including the sub-directory of the Judgment

     Public Static voidMain (string[] args) {File dir=NewFile ("f:/");    Allfilesearch (dir); }         Public Static voidAllfilesearch (File dir) {file[] Subfiles = Dir.listfiles ();//get all child files, including files and paths         for(File subfile:subfiles) {//Traverse            if(Subfile.isdirectory ()) {//is directory, recursive lookupAllfilesearch (subfile); }Else{//is the file that determines the direct output                if(Subfile.getname (). EndsWith (". txt") {System.out.println (subfile); }            }        }    }

 

    

Java Foundation-file Class

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.