android 怎么写文件日志到SD卡上
答案:1 悬赏:0
解决时间 2021-02-18 06:29
- 提问者网友:伴他一生,无悔
- 2021-02-17 10:53
android 怎么写文件日志到SD卡上
最佳答案
- 二级知识专家网友:如果这是命
- 2021-02-17 12:26
android 如何写文件日志到SD卡上..
public void writeFileToSD(String context) {
//使用RandomAccessFile 写文件 还是蛮好用的..推荐给大家使用...
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
Log.d("TestFile", "SD card is not avaiable/writeable right now.");
return;
}
try {
String pathName = "/sdcard/";
String fileName = "log.txt";
File path = new File(pathName);
File file = new File(pathName + fileName);
if (!path.exists()) {
Log.d("TestFile", "Create the path:" + pathName);
path.mkdir();
}
if (!file.exists()) {
Log.d("TestFile", "Create the file:" + fileName);
file.createNewFile();
}
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(file.length());
raf.write(context.getBytes());
raf.close();
//注释的也是写文件..但是每次写入都会把之前的覆盖..
} catch (Exception e) {
Log.e("TestFile", "Error on writeFilToSD.");
}
}
public void writeFileToSD(String context) {
//使用RandomAccessFile 写文件 还是蛮好用的..推荐给大家使用...
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
Log.d("TestFile", "SD card is not avaiable/writeable right now.");
return;
}
try {
String pathName = "/sdcard/";
String fileName = "log.txt";
File path = new File(pathName);
File file = new File(pathName + fileName);
if (!path.exists()) {
Log.d("TestFile", "Create the path:" + pathName);
path.mkdir();
}
if (!file.exists()) {
Log.d("TestFile", "Create the file:" + fileName);
file.createNewFile();
}
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(file.length());
raf.write(context.getBytes());
raf.close();
//注释的也是写文件..但是每次写入都会把之前的覆盖..
} catch (Exception e) {
Log.e("TestFile", "Error on writeFilToSD.");
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯