Android存储Log日志到SD卡

释放双眼,带上耳机,听听看~!
/**
 *读取文件
 */
private String read() {
String path = "/data/data/你得包名/logs/";
String name = "x.txt";
BufferedReader reader = null;
    try {
FileInputStream f = new FileInputStream(path + name);
reader = new BufferedReader(new InputStreamReader(f));
String line;
String lines = "";
        while ((line = reader.readLine()) != null) {
line += "rn";
lines = lines + line;
}
return lines;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "...........";
}
//检测路径是否存在
private void isHavePath(String path) {
File file = new File(path);
    if (!file.exists()) {
file.mkdirs();
}
}
//获取 log 日志的线程,保存成 text
@Override
public void run() {
String path = "/data/data/你程序的包名/logse/";
String name = "x.txt";
    try {
//获取logcat日志信息
isHavePath(path);
File file = new File(path + name);
String str = "hello world";
FileWriter fw = new FileWriter(file + "as.txt");
fw.flush();
fw.write(str);
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
不要忘记添加权限
<uses-permission android:name="android.permission.READ_LOGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
大家加油

人已赞赏
Android文章

Android 全局异常捕获 重启app

2020-3-8 6:28:44

Android文章

Android ListView这类的滑动控件,设置滚动条在左侧

2020-3-8 8:21:48

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索