1. 程式人生 > >Android內部儲存和外部儲存以及快取清理和記憶體清理!

Android內部儲存和外部儲存以及快取清理和記憶體清理!

android內部的儲存結構和路徑獲取方法:

清除本地資料和快取:

計算快取和記憶體資料的大小:

計算快取大小以及刪除的方法:

這文章裡面的檔案刪除方法有一些問題:沒有迴圈是資料夾的刪除方法;另外:

File.list(); 獲取的是該資料夾下的所有子檔案的相對路徑;

File.listFiles();獲取的是該資料夾下的所有子檔案的絕對路徑;

android的檔案儲存工具類:

import android.content.Context;
import android.os.Environment;
import android.util.Xml;

import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

public  class FileService {
    /** 
     * save data to phone rom 
     * @param context  上下文 
     * @param fileName  儲存的檔名 
     * @param name      使用者名稱 
     * @param password   密碼 
     * @return 
     */  
   public static boolean saveToRom(Context context, String fileName, String name, String password){
        // File file = new File("/data/data/cn.itcast.login/a.txt");  
        //相當於儲存到/data/dat/packageName/目錄下  
        File file = new File(context.getExternalFilesDir("test"), fileName);
        // 如果沒有指定訪問的模式 ,檔案的模式 預設是私有的許可權.  
        // 只有當前的應用程式可以讀寫這個檔案 ,別的應用程式是不可以操作這個檔案.  
       try {  
        FileOutputStream fos=new FileOutputStream(file);
        fos.write((name+":"+password).getBytes());  
        fos.close();  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
   }  
     
   /** 
     * 儲存資料到手機的rom空間的快取目錄 
     * 作用 儲存應用程式的臨時資料, 在手機記憶體不足的時候 系統會釋放掉這塊空間 
     * 使用者也可以手工的釋放掉這塊空間 
     * @param context 上下文
     * @param name 使用者名稱 
     * @param password 密碼 
     * @return 
     */  
   public static boolean saveToRomCache(Context context,String fileName,String name,String password){  
        File file=new File(context.getCacheDir(),fileName);///data/dat/packageName/  
           try {  
            FileOutputStream fos=new FileOutputStream(file);  
            fos.write((name+":"+password).getBytes());  
            fos.close();  
            return true;  
        } catch (Exception e) {  
            e.printStackTrace();  
            return false;  
        }  
  }  
     
   /** 
    * sava data to externalStorage【外部儲存卡】 
    * @param context 
    * @param fileName 
    * @param name 
    * @param password 
    * @return 
    */  
   public static boolean saveToSD(Context context,String fileName,String name,String password){  
        //相當於儲存到/mnt/sdcard/目錄下  
       //在儲存資料到sd卡之前 ,最好判斷一下 使用者是否有sd卡 sd是否可用.  
        File file=new File(Environment.getExternalStorageDirectory(),fileName);
        try {  
            FileOutputStream fos=new FileOutputStream(file);  
            fos.write((name+":"+password).getBytes());  
            fos.close();  
            return true;  
        } catch (Exception e) {  
            e.printStackTrace();  
            return false;  
        }  
   }  
     
   /** 
    * 將使用者輸入的資料以xml檔案格式儲存到手機rom空間 
    * @param context 
    * @param name 
    * @param password 
    * @return 
    */  
   public static boolean saveToXML(Context context,String name,String password){  
       File file=new File(context.getFilesDir(),"info.xml");  
       try {  
        FileOutputStream fos=new FileOutputStream(file);  
        XmlSerializer serial= Xml.newSerializer();
        //初始化一下xml的序列化器  
        serial.setOutput(fos, "UTF-8");  
        serial.startDocument("UTF-8", true);  
        serial.startTag(null, "map");  
          
        serial.startTag(null, "name");  
        serial.text(name);  
        serial.endTag(null, "name");  
          
        serial.startTag(null, "password");  
        serial.text(password);  
        serial.endTag(null, "password");  
          
        serial.endTag(null, "map");  
        serial.endDocument();  
        fos.flush();  
        fos.close();  
        return true;  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  
   }  
     
   /** 
    * 從rom檔案中讀取儲存的內容 
    * @param context 
    * @param fileName 
    * @return 
    */  
   public static Map<String,String> readFromRom(Context context, String fileName){
       File file=new File(context.getExternalFilesDir("test"),fileName);
       try {  
        FileInputStream fis=new FileInputStream(file);
        byte[] result=StreamTools.getBytes(fis);  
        String[] data=new String(result).split(":");
           if (data!=null&&data.length>=2){
        String name=data[0];  
        String password=data[1];
               Map<String,String> map=new HashMap<String, String>();
               map.put("name", name);
               map.put("password", password);
               return map;
           }
      return new  HashMap<String, String>();
    } catch (Exception e) {  
        e.printStackTrace();  
        return null;  
    }  
   }  
 }