Android development accumulation _ 1) Get files, folder buckets, and use Toast in a user-friendly manner

Source: Internet
Author: User

Android development accumulation _ 1) Get files, folder buckets, and use Toast in a user-friendly manner

Record the file-related content that needs to be used frequently. In other words, good memory is not as good as bad writing!

1)

/*** Obtain the total memory space of a file or folder in the path *** @ return */public static long getTotalMemorySize (String path) {StatFs stat = new StatFs (path); long blockSize = stat. getBlockSize (); // The number of nodes occupied by each block long totalBlocks = stat. getBlockCount (); // The total number of blocks return totalBlocks * blockSize ;}


2)
/*** Obtain the memory space of a file or folder Based on the path. ** @ return */public static long getAvailableMemorySize (String path) {StatFs stat = new StatFs (path); long blockSize = stat. getBlockSize (); long availableBlocks = stat. getAvailableBlocks (); return availableBlocks * blockSize ;}

3)

/*** Get the size of the specified File ** @ param f * @ return * @ throws Exception */private static long getFileSize (file File file) {long size = 0; if (file. exists () {FileInputStream FD = null; try {FD = new FileInputStream (file); // use FileInputStream to read data streams of file size = Fi. available (); // file size} catch (IOException e) {e. printStackTrace ();} finally {try {FCM. close ();} catch (IOException e) {e. printStackTrace () ;}} else {Log. e (TAG ," File not exists! ");} Return size ;}

4)

/*** Get the size of the specified folder ** @ param f * @ return * @ throws Exception */private static long getFileSizes (File f) {long size = 0; file flist [] = f. listFiles (); // all files in the folder directory for (int I = 0; I <flist. length; I ++) {if (flist [I]. isDirectory () {// determine whether there are subdirectories under the parent directory size = size + getFileSizes (flist [I]);} else {size = size + getFileSize (flist [I]) ;}} return size ;}

5)This method prevents multiple pop-up reminders caused by multiple Toast triggers.

private Toast mToast = null;private void showToast(Context context, String str) {if (mToast == null) {mToast = Toast.makeText(context, str, Toast.LENGTH_SHORT);} else {mToast.setText(str);}mToast.show();}

File-related operations:


1) String Name = File. getName (); // obtain the Name of the File or folder:
2) File. mkDir (); // create a folder
3) File. createNewFile (); // create a File
4) File. isDirectory (); // identifies a File or folder.
5) File. isDirectory ()
6) File [] files = File. listFiles (); // list all files and folder names in the folder
7) File. renameTo (dest); // modify the folder and File name
8) File. delete (); // delete a folder or File
9) childFiles [I]. getName (). contains ("testfolder"); // determines whether a file name contains "testfolder"




Related Article

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.