File class (java)

Source: Internet
Author: User

List System partitions and C root directories in java

 

Package com. limeteor. demo;

Import java. io. File;
Import java. text. SimpleDateFormat;
Import java. util. Date;

/**
* File class
* @ Author limeteor
*
*/
Public class FileDemo {

Public static SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd ");

Public static void main (String [] args ){

// Obtain all Disk Partitions of the current File System
File [] disks = File. listRoots ();
System. out. println ("System disk partition :");
For (int I = 0; I <disks. length; I ++ ){
File disk = disks [I];
System. out. println (disk. getAbsolutePath () + "Total disk \ t Space :"
+ Disk. getTotalSpace ()/1024/1024/1024 + "G \ t remaining space"
+ Disk. getFreeSpace ()/1024/1024/1024 + "G ");
}
// Obtain the C root directory
File c = new File ("C :");
If (! C. exists ()){
System. out. println ("no disk C found ");
Return;
}
System. out. println ("C root directory structure :");
File [] files = c. listFiles ();
For (int I = 0; I <files. length; I ++ ){
File file = files [I];
If (file. isDirectory ()){
System. out. println ("[Directory]" + file. getName ());
}
If (file. isFile ()){
System. out. println ("[file]" + file. getName ());
System. out. println ("\ t size" + file. length ()/1024 + "K ");
Date date = new Date (file. lastModified ());
System. out. println ("\ t modification date" + sdf. format (date) + "\ t ");
If (file. isHidden ()){
System. out. println ("[hide]");
}
If (! File. canWrite ()){
System. out. println ("[Read-Only]");
}
System. out. println ();
}
}
}
}

 

Knowledge points used

1. The File class is in the java. io package and must be referenced before use.

2. The listModified method of the File class returns the number of milliseconds since January 1, January 1, 1970. You need to convert the value to the Date type before formatting the output.

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.