Android calculates the folder size and deletes the folder and its content.

Source: Internet
Author: User

[Java]/*** get the folder size * @ param file File instance * @ return long in the unit of M * @ throws Exception */public static long getFolderSize (java. io. file file) throws Exception {long size = 0; java. io. file [] fileList = file. listFiles (); for (int I = 0; I <fileList. length; I ++) {if (fileList [I]. isDirectory () {size = size + getFolderSize (fileList [I]);} else {size = size + fileList [I]. length () ;}} return size/1048576;} [Java]/*** delete files and directories in the specified directory ** @ param deleteThisPath * @ param filepath * @ return */public void deleteFolderFile (String filePath, boolean deleteThisPath) throws IOException {if (! TextUtils. isEmpty (filePath) {File file = new File (filePath); if (file. isDirectory () {// process directory File files [] = file. listFiles (); for (int I = 0; I <files. length; I ++) {deleteFolderFile (files [I]. getAbsolutePath (), true);} www.2cto.com} if (deleteThisPath) {if (! File. isDirectory () {// if it is a file, delete the file. delete ();} else {// directory if (file. listFiles (). length = 0) {// No file or directory exists in the directory. delete the file. delete ();}}}}}

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.