android檔案處理

來源:互聯網
上載者:User

擷取檔案大小 

  1. public   static  final  int  MAX_ATTACHMENT_UPLOAD_SIZE = (5 * 1024 * 1024);  
  2.   
  3. File file = new  File( "/sdcard/default/aaa.txt" );   
  4. long  size =  file.length();   //擷取檔案大小,單位為bytes   
  5. if (size > Email.MAX_ATTACHMENT_UPLOAD_SIZE)   //判斷檔案大小,最大5MB = 5 * 1024 * 1024 bytes   
  6. {  
  7.      Toast.makeText(this ,  "File exceed 5MB, too large to attach" ,Toast.LENGTH_LONG)  
  8.                     .show();          




判斷檔案尾碼

  1. File folder =  new  File( "/sdcard/data" );  
  2. PickerFilter filter = new  PickerFilter();  
  3. File [] files = folder.listFiles(filter);  
  4. int  fileCount = files.length;     //擷取目前的目錄下檔案的個數   
  5. String fileName = files[0].getName(); //擷取第一個檔案的名字   
  6.   
  7. /* 先將檔案名稱轉換為小寫,因為endsWith函數會區分大小寫,若尾碼為.Txt,  
  8.    判斷時會出現不匹配於.txt導致判斷有誤 */   
  9. fileName = fileName.toLowerCase();  
  10. boolean isTxt = fileName.endsWith(".txt" );   



android在SD卡上建立檔案儲存資訊

     1)判斷是否存在SD卡

     2)擷取SD卡目錄

     3)在SD卡目錄下建立檔案

     4)寫入資訊到檔案中

代碼:

  1. //判斷SD卡是否存在   
  2. boolean sdCardExist = Environment.getExternalStorageState()  
  3.                       .equals(android.os.Environment.MEDIA_MOUNTED)  
  4. if  (sdCardExist)  
  5. {                                 
  6.     //擷取SD卡目錄   
  7.     File sdDir = Environment.getExternalStorageDirectory();  
  8.     //在SD卡目錄下建立檔案smsLog.txt檔案,true表示當檔案存在時,資訊追加在檔案尾   
  9.     FileWriter fw = new  FileWriter( sdDir.toString() +  "/smsLog.txt" ,  true );   
  10.     //擷取目前時間   
  11.     Calendar calendar = Calendar.getInstance();  
  12.     Date d = calendar.getTime();  
  13.   
  14.     fw.write("簡訊接收時間:"  + d.toString());  
  15.     fw.write("\r\n" );  //寫入換行     
  16.      fw.write("簡訊內容:" );  
  17.     fw.write("\r\n" );  
  18.        
  19.     //關閉檔案   
  20.     fw.close();           
  21.   } 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.