概述
package com.android.write;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class SaveLog extends Thread {
private String shell = "logcat";
private String logpath = "/mnt/sdcard2/logcat.txt";
private BufferedReader mReader;
private Process process = null;
private FileOutputStream fos = null;
private InputStream inputStream= null;
private boolean mRunning = true;
private BufferedWriter bw = null;
private File file = null;
public SaveLog() {
file = new File(logpath);
try {
if (!file.exists()) {
file.createNewFile();
}
fos = new FileOutputStream(file, true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
try {
process = Runtime.getRuntime().exec(shell);
inputStream = process.getInputStream();
mReader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while ((line = mReader.readLine()) != null){
file = new File(logpath);
if (!file.exists()) {
file.createNewFile();
fos = new FileOutputStream(file, true);
}
if (file.length() > 1 * 1024 * 1024) {
FileOutputStream fos = new FileOutputStream(file);
fos.write("".getBytes());
fos.close();
clearLogCache();
}
if (line.length() == 0) {
continue;
}
if (fos != null) {
fos.write((line + "n").getBytes());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void clearLogCache() {
Process proc = null;
List commandList = new ArrayList();
commandList.add("logcat");
commandList.add("-c");
//commandList.add("*:E");// 过滤所有的错误信息
try {
proc = Runtime.getRuntime().exec(
commandList.toArray(new String[commandList.size()]));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
最后
以上就是舒服柠檬为你收集整理的android log保存文件,android logcat日志保存到文件中的全部内容,希望文章能够帮你解决android log保存文件,android logcat日志保存到文件中所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复