/**
*读取文件
*/
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 += "\r\n";
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"/>
大家加油