Package Tony;
Import java. Io. file;
Import java. Text. decimalformat;
Import java. Io. fileinputstream;
Public class getfilesize
{
Public longgetfilesizes (file F) throws exception {// get the file size
Long S = 0;
If (F. exists ()){
Fileinputstream FCM = NULL;
FS = new fileinputstream (f );
S = FCM. Available ();
} Else {
F. createnewfile ();
System. Out. println ("file does not exist ");
}
Return S;
}
// Recursion
Public longgetfilesize (file F) throws exception // get the folder size
{
Long size = 0;
File flist [] = f. listfiles ();
For (INT I = 0; I <flist. length; I ++)
{
If (flist [I]. isdirectory ())
{
Size = size + getfilesize (flist [I]);
} Else
{
Size = size + flist [I]. Length ();
}
}
Return size;
}
Publicstring formetfilesize (long files) {// convert the file size
Decimalformat df = new decimalformat ("#. 00 ");
String filesizestring = "";
If (Files <1024 ){
Filesizestring = DF. Format (double) files) + "B ";
} Else if (Files <1048576 ){
Filesizestring = DF. Format (double) files/1024) + "K ";
} Else if (Files <1073741824 ){
Filesizestring = DF. Format (double) files/1048576) + "M ";
} Else {
Filesizestring = DF. Format (double) files/1073741824) + "G ";
}
Return filesizestring;
}
Public longgetlist (file F) {// recursively calculates the number of directory files
Long size = 0;
File flist [] = f. listfiles ();
Size = flist. length;
For (INT I = 0; I <flist. length; I ++ ){
If (flist [I]. isdirectory ()){
Size = size + getlist (flist [I]);
Size --;
}
}
Return size;
}
Publicstatic void main (string ARGs [])
{
Getfilesize G = new getfilesize ();
Long starttime = system. currenttimemillis ();
Try
{
Long L = 0;
String Path = "C :\\ windows ";
File FF = new file (PATH );
If (FF. isdirectory () {// if the path is a folder
System. Out. println ("number of files "+ G. getlist (ff ));
System. Out. println ("directory ");
L = G. getfilesize (ff );
System. Out. println (path + "directory size:" + G. formetfilesize (l ));
} Else {
System. Out. println (" Number of Files 1 ");
System. Out. println ("file ");
L = G. getfilesizes (ff );
System. Out. println (path + "file size:" + G. formetfilesize (l ));