Java obtains the folder size, file size, and number of files.

Source: Internet
Author: User

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 ));
          

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.