Use of the File class in java and the java File class

Source: Internet
Author: User

Use of the File class in java and the java File class

Public class FileLei {

Public static void main (String [] args) throws IOException {
// .. Indicates the upper-level directory. indicates the current directory.
File file = new File ("C: \ Users \ cdlx2016 \ Desktop \ file class ");
System. out. println ("Whether the file exists:" + file. exists ());

File file1 = new File (".. \ a.txt ");
System. out. println (file1.getAbsolutePath ());
System. out. println ("Whether the file exists:" + file1.exists ());

// Create an empty file at the specified location. An empty file with the same name cannot be created.
File file2 = new File ("C: \ Users \ cdlx2016 \ Desktop \ file class \ aa ");
System. out. println ("Create an empty file through the specified path:" + file2.createNewFile ());

File file3 = new File ("C: \ Users \ cdlx2016 \ Desktop \ file class \ cc ");
System. out. println ("created successfully:" + file3.mkdir ());

File file4 = new File ("C: \ Users \ cdlx2016 \ Desktop \ file class \ dd ");
System. out. println ("created successfully:" + file4.mkdirs ());

// Rename
File file5 = new File ("C: \ Users \ cdlx2016 \ Desktop \ file class \ a.txt ");
System. out. println ("Whether the file exists:" + file5.exists ());
File file6 = new File ("C: \ Users \ cdlx2016 \ Desktop \ file class \ abc.txt ");
System. out. println ("renamed successfully:" + file5.renameTo (file6 ));

// Change the drive letter
File file7 = new File ("C: \ Users \ cdlx2016 \ Desktop \ file class \ abc.txt ");
System. out. println ("Successful modification:" + file7.renameTo (new File ("D: \ abc.txt ")));

// Delete, Which is deleted when the jvm exits.
File file8 = new File ("C: \ Users \ cdlx2016 \ Desktop \ file class \ abc.txt ");
System. out. println ("successful deletion:" + file8.delete ());

// Determine whether it is a file or a folder
File file9 = new File ("C: \ Users \ cdlx2016 \ Desktop \ file class \ aa ");
System. out. println ("whether it is a file:" + file9.isFile ());
System. out. println ("whether it is a folder:" + file9.isDirectory ());
System. out. println ("Hide:" + file9.isHidden ());
System. out. println ("whether it is an absolute path:" + file9.isAbsolute ());

// Check whether a folder contains a hidden folder or file
// 1. Obtain all files or folders
// 2. Final judgment through the isHidden () method
System. out. println ("Get name:" + file9.getName ());
System. out. println ("obtain path:" + file9.getPath ());
System. out. println ("Get absolute path:" + file9.getAbsolutePath ());

// Obtain the object size in bytes.
File file10 = new File ("C: \ Users \ cdlx2016 \ Desktop \ file class \ AB .txt ");
System. out. println ("Get file size:" + file10.length ());
System. out. println ("retrieve parent directory:" + file10.getParent ());

// Obtain the last modification time, in milliseconds
Long time = file10.lastModified ();
Date date = new Date (time );
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-mm-dd HH: mm: ss ");
System. out. println ("Last modified time:" + dateFormat. format (date ));

// Folder operations
File [] files = File. listRoots (); // obtain all the drive letters on the computer
For (File f: files ){
System. out. println ("drive letter" + f );
}

// Obtain the files and subdirectories (including hidden ones) in the specified folder)
File file11 = new File ("C: \ Users \ cdlx2016 \ Desktop \ file class ");
String [] nameString = file. list ();
For (String str: nameString ){
System. out. println (str );
}

File file12 = new File ("C: \ Users \ cdlx2016 \ Desktop \ Java part (javaSE )");
File [] files2 = file12.listFiles ();
For (File file13: files2 ){
If (file13.isDirectory ()){

} Else {
System. out. println (file13.getName ());
}
}

// View the file with the specified suffix
File file14 = new File ("C: \ Users \ cdlx2016 \ Desktop \ file class ");
String [] strings01 = file14.list (new MyFilter ());
For (String str01: strings01 ){
System. out. println (str01 );
}
}
}

Class MyFilter implements FilenameFilter {
@ Override
Public boolean accept (File dir, String name ){
// Extract the string following the last vertex and compare it with. javajava
Name = name. substring (name. lastIndexOf ('.') + 1 ));
Return "txt". equals (name );
}
}

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.