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.