Use of the file class in Java

Source: Internet
Author: User
Tags dateformat

public class Filelei {

public static void Main (string[] args) throws IOException {
//.. Represents the previous level of 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 ());

Creates an empty file at the specified location and cannot create a
File File2 = new File ("C:\\users\\cdlx2016\\desktop\\file class \\aa");
System.out.println ("Creates an empty file with the specified path:" +file2.createnewfile ());

File File3 = new File ("C:\\users\\cdlx2016\\desktop\\file class \\CC");
System.out.println ("Whether to create success:" +file3.mkdir ());

File File4 = new File ("C:\\users\\cdlx2016\\desktop\\file class \\dd");
System.out.println ("Whether to create success:" +file4.mkdirs ());

Renaming
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 ("Rename succeeded:" +file5.renameto (File6));

Change the drive letter
File File7 = new File ("C:\\users\\cdlx2016\\desktop\\file class \\abc.txt");
System.out.println ("Change succeeded:" +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 ("Delete succeeded:" +file8.delete ());

Decide if it's 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 ("Is folder:" +file9.isdirectory ());
System.out.println ("Whether Hidden:" +file9.ishidden ());
System.out.println ("is absolute path:" +file9.isabsolute ());

See if a folder contains hidden folders or files
1. By getting all the files or folders
2. Finally, the Ishidden () method is used to determine
System.out.println ("Get Name:" +file9.getname ());
SYSTEM.OUT.PRINTLN ("Get Path:" +file9.getpath ());
System.out.println ("Get absolute Path:" +file9.getabsolutepath ());

Gets the size of the file 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 ("Get Parent Directory:" +file10.getparent ());

Gets the time, in milliseconds, for the last modification
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));

Operation of the folder
file[] files = file.listroots ();//Get all the drive characters in your computer
for (File f:files) {
SYSTEM.OUT.PRINTLN ("drive letter" +f);
}

Gets the files and subdirectories under the specified folder (contains hidden)
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 section (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 name
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) {
The idea is to intercept the last point after the string and the. Javajava do a comparison
Name = name.substring (Name.lastindexof ('. ') +1));
Return "TXT". Equals (name);
}
}

Use of the file class in Java

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.