Android development accumulate _1) get the user-friendly use of files, folder storage space and Toast

Source: Internet
Author: User

Record a often need to apply to the file related content, saying good memory than bad writing!

1)

<strong>/** * Based on the path, the total size of memory space of the memory in which a file or folder resides *  * @return */public static long gettotalmemorysize (String path) { StatFs stat = new StatFs (path); Long blockSize = Stat.getblocksize (); Each block occupies a number of bytes long totalblocks = Stat.getblockcount (); Block Total Return totalblocks * blockSize;} </strong>


2)
/** * Based on path, how much memory space for a file or folder is available *  * @return */public static long getavailablememorysize (String path) {StatFs s tat = new StatFs (path); Long blockSize = Stat.getblocksize (); Long availableblocks = Stat.getavailableblocks (); return Availableblocks * BLOCKSIZE;}

3)

/** * Gets the size of the specified file *  * @param f * @return * @throws Exception */private static long getfilesize (file file) {Long size = 0 if (file.exists ()) {FileInputStream FIS = null;try {fis = new FileInputStream (file);//Use FileInputStream to read the data stream in file size = Fis.available ();//size of File} catch (IOException e) {e.printstacktrace ();} finally{try {Fis.close ();} catch (IOException e) { E.printstacktrace ();}}} else {LOG.E (TAG, "File not exists!");} return size;}

4)

/** * Gets the size of the specified folder *  * @param f * @return * @throws Exception */private static long getfilesizes (File f) {long size = 0; File flist[] = F.listfiles ()///folder directory for all files for (int i = 0; i < flist.length; i++) {if (Flist[i].isdirectory ()) {//= parent) There's a subdirectory. Size = size + getfilesizes (Flist[i]);} else {size = size + getfilesize (Flist[i]);}} return size;}

5)Use this method to prevent multiple pop-up reminders when a toast is triggered multiple times

Private Toast mtoast = null;private void Showtoast (context context, String str) {if (mtoast = = null) {Mtoast = Toast.maket EXT (context, str, toast.length_short);} else {mtoast.settext (str);} Mtoast.show ();}

File related actions:


1) String name = File.getname ();// get the file or folder names:
2) File.mkdir (); Create a folder
3) File.createnewfile ();//Build file
4) file.isdirectory ();// judgment is a file or folder
5) File.isdirectory ()
6) file[] files = file.listfiles ();// list all file and folder names under Folders
7) File.renameto (dest);// Modify folder and file name
8) File.delete ();// Delete a folder or file
9) Childfiles[i].getname (). Contains ("TestFolder");// determine if a file name contains "TestFolder"




Android Development accumulate _1) get the user-friendly use of files, folder storage, and toasts

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.