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 红包祝福语简短10字
18 周六早晨祝福语简短