本文实例讲述了Android SD卡上文件操作及记录日志操作的方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
// 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程序设计有所帮助。