Android Gets the specified file size

Source: Internet
Author: User

/** * Gets the specified file size  * @param f  * @return  * @throws Exception */public static long getfilesize (file file) throws Exception {Long size = 0;if (File.exists ()) {FileInputStream FIS = Null;fis = new FileInputStream (file); size = Fis.availab Le ();} else {file.createnewfile (); LOG.E ("Get File Size", "file does not exist!"); return size;}


/** * Get the specified folder * @param f * @return * @throws Exception *  */public static long getfilesizes (File f) throws Exception {L Ong size = 0; File flist[] = F.listfiles (); for (int i = 0; i < flist.length; i++) {if (Flist[i].isdirectory ()) {size = size + GetFile Sizes (Flist[i]);} else {size = size + getfilesize (Flist[i]);}} return size;}

/** * Convert File Size * @param files * @return *  */public static String formetfilesize (long files) {DecimalFormat df = new Deci Malformat ("#.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 {filesizestring = Df.f Ormat (double) files/1073741824) + "GB";} return filesizestring;}



Android Gets the specified file size

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.