Android get folder, file size in B, KB, MB, GB

Source: Internet
Author: User

Android get folder, file size in B, KB, MB, GB

 public class Filesizeutil {public static final int sizetype_b = 1;//Gets the double value of file size unit B public static final int sizety pe_kb = 2;//Gets the file size in kilobytes of double value public static final int sizetype_mb = 3;//Gets the file size unit to MB Double value public static final int sizety PE_GB = 4;//Gets the file size unit is a double value of GB/*** gets the size of the specified unit of file specified by the file * @param filePath file path * @param sizeType gets the size of type 1 for B, 2 KB, 3 MB, 4 for gb* @ Return the size of the double value */public static double getfileorfilessize (String filepath,int sizeType) {file File=new file (FilePath); Long Blocksize=0;try {if (File.isdirectory ()) {blockSize = getfilesizes (file);} Else{blocksize = getfilesize (file);}} catch (Exception e) {e.printstacktrace (); LOG.E ("Get File Size", "Get failed!");} Return Formetfilesize (BlockSize, sizeType);} /*** call this method to automatically calculate the size of the specified file or the specified folder * @param filePath file path * @return calculated string with B, KB, MB, GB, */public static string Getautofileorfilessize (String filePath) {file File=new file (FilePath); long Blocksize=0;try {if (File.isdirectory ()) { BlockSize = getfilesizes (file);} Else{blocksize = getfilesize (file);}} catch (EXception e) {e.printstacktrace (); LOG.E ("Get File Size", "Get failed!");} Return Formetfilesize (blockSize);} /*** gets the specified file size * @param f* @return * @throws exception*/private static long getfilesize (file file) throws Exception{long size = 0; if (File.exists ()) { fileinputstream FIS = Null; fis = new FileInputStream (file);  size = Fis.ava Ilable ();  } else{ file.createnewfile (); &NBSP;LOG.E ("Get File Size", "file does not exist!");  } return size;}  /*** gets the specified folder * @param f* @return * @throws exception*/private static long getfilesizes (File f) throws Exception{long S ize = 0; File flist[] = F.listfiles (); for (int i = 0; i < flist.length; i++) {if (Flist[i].isdirectory ()) {size = size + Getfilesi Zes (Flist[i]);} Else{size =size + getfilesize (Flist[i]);}} return size;} /** * Convert File size  * @param files * @return  */private static String formetfilesize (long files) { DecimalFormat df = new DecimalFormat ("#.00"); String filesizestring = ""; String wrongsize= "0B"; if (files==0) {return wrongsize;} if (Files < 1024x768) {filesizestring = Df.format (double) fileS) + "B";  }else if (Files < 1048576) {filesizestring = Df.format (double) files/1024) + "KB";} else if (FileS < 1073741824) {    filesizestring = Df.format ((double) files/1048576) + "MB"; }else{&nb Sp   filesizestring = Df.format ((double) files/1073741824) + "GB"; }return filesizestring;} /** * Convert file size, specify the type of conversion  * @param files  * @param sizetype  * @return  */private Static Double formetfilesize (Long Files,int sizeType) {DecimalFormat df = new DecimalFormat ("#.00");d ouble filesizelong = 0; Switch (sizeType) {case sizetype_b:filesizelong=double.valueof (Df.format (Double) fileS); Break;case sizetype_kb: Filesizelong=double.valueof (Df.format (Double) files/1024); Break;case sizetype_mb:filesizelong=double.valueof ( Df.format (double) files/1048576) Break;case sizetype_gb:filesizelong=double.valueof (Df.format (double) FileS/ 1073741824)); break;default:break;} return Filesizelong;}

Android Get folder, file size in B, KB, MB, GB

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.