Android SD卡上文件操作及记录日志操作实例分析
本文实例讲述了Android SD卡上文件操作及记录日志操作的方法。分享给大家供大家参考,具体如下:
// SD卡是否存在 private boolean checkSDCardStatus() { boolean SDCardStatus = false; String sDStateString = android.os.Environment.getExternalStorageState(); if (sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) { SDCardStatus = true; } else { // SD卡不可用 } return SDCardStatus; } // SD卡上是否有APK文件 private boolean checkFileExist(String iFilePath) { boolean fileExist = false; if (checkSDCardStatus()) { File myFile = new File(iFilePath); if (myFile.exists()) { fileExist = true; } } return fileExist; } //删除文件 private void deleteApk(String iFilePath) { if (checkSDCardStatus()) { File myFile = new File(iFilePath); if (myFile.exists()) { myFile.delete(); } } } //在SD卡上用txt记录 public static void logOnFile(String format, Object... args){ String logstr = String.format(format, args); SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = sDateFormat.format(new java.util.Date()); logstr = "/r/n-------------------------/r/n" + date + "/r/n" + logstr; FileOutputStream fout; DataOutputStream dataout; try { fout = new FileOutputStream("//sdcard//log.txt",true); dataout = new DataOutputStream(fout); dataout.writeUTF(logstr); dataout.flush(); dataout.close(); fout.flush(); fout.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
希望本文所述对大家Android程序设计有所帮助。
赞 (0)