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"