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 妈妈生日祝福语简短励志