Java basics-File Operations

Source: Internet
Author: User
Java basics file operations-Linux general technology-Linux programming and kernel information, the following is a detailed description. Java. io. File (File class)

Although most classes defined by java. io are stream operations, the File class is not. It processes files and file systems directly. That is to say, the File class does not specify how information is read from or stored in a File. A File object is used to obtain or process information related to a disk File, such as permission, time, date, and directory path. In addition, File also browses the directory hierarchy.

[Constructor]

File (String directoryPath) File f1 = new File ("/")

File (String directoryPath, String filename) File f1 = new File ("/", "auto. bat ")

File (File dirObj, String filename) File f1 = new File (f1, "auto. bat ")



[Method]

CanWrite (), canRead (), exists () whether a file exists, getName () returns the file name, getParent () returns the parent directory, getPath (), getAbsolutePath (), isFile (), isAbsolute (), length (), lastModified ()

These methods allow you to verify the attributes of a simple file object, but there is no corresponding function to change these attributes.

RenameTo (), delete () ---- used to operate files.



[Directory]

A directory is a File class that contains other files and path lists. If a File object is a directory, you can use the isDirectory () method and list () method. The Mkdir () method is used to create a directory. The mkdirs () method is used to create a directory and all the parent directories of the directory.



[List method]

1) String [] list ()

2) String [] list (FilenameFilter FFObj). FFObj is the object of the class implementing the FilenameFilter interface.

The FilenameFilter interface has a method boolean accept (File directory, String filename ). This method is called once by each file in the list.

Import java. io .*;

Public class OnlyExt implements FilenameFilter {

String ext;

Public OnlyExt (String ext ){

This. ext = "." + ext;

}

Public Boolean accept (File dir, String name ){

Return name. endsWith (ext );

}

}

// Only files with the extension of .html are displayed.

Import java. io .*;

Class DirListOnly {

Public static void main (String args []) {

String dirname = "/java ";

File f1 = new File (dirname );

FilenameFilter only = new OnlyExt ("html ");

String s [] = f1.list (only );



For (int I = 0; I
System. out. println (s [I]);

}

}

}


[ListFiles method]

Java2 adds a Variant Form of the list () method, called listFiles (), in the following format:

File [] listFiles ();

File [] listFiles (FilenameFilter FFObj );

File [] listFiles (FileFilter Fojb );

The third form returns the file that meets the path name of the specified FileFilter. FileFilter defines only one accept method, which is called once by each file in the list.

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.