Android SD卡上文件操作及记录日志操作实例分析
本文实例讲述了AndroidSD卡上文件操作及记录日志操作的方法。分享给大家供大家参考,具体如下:
//SD卡是否存在
privatebooleancheckSDCardStatus(){
booleanSDCardStatus=false;
StringsDStateString=android.os.Environment.getExternalStorageState();
if(sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)){
SDCardStatus=true;
}else{
//SD卡不可用
}
returnSDCardStatus;
}
//SD卡上是否有APK文件
privatebooleancheckFileExist(StringiFilePath){
booleanfileExist=false;
if(checkSDCardStatus()){
FilemyFile=newFile(iFilePath);
if(myFile.exists()){
fileExist=true;
}
}
returnfileExist;
}
//删除文件
privatevoiddeleteApk(StringiFilePath){
if(checkSDCardStatus()){
FilemyFile=newFile(iFilePath);
if(myFile.exists()){
myFile.delete();
}
}
}
//在SD卡上用txt记录
publicstaticvoidlogOnFile(Stringformat,Object...args){
Stringlogstr=String.format(format,args);
SimpleDateFormatsDateFormat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
Stringdate=sDateFormat.format(newjava.util.Date());
logstr="/r/n-------------------------/r/n"+date+"/r/n"+logstr;
FileOutputStreamfout;
DataOutputStreamdataout;
try{
fout=newFileOutputStream("//sdcard//log.txt",true);
dataout=newDataOutputStream(fout);
dataout.writeUTF(logstr);
dataout.flush();
dataout.close();
fout.flush();
fout.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
希望本文所述对大家Android程序设计有所帮助。
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短