重要的点
- 把错误信息保存到一个String变量中
public static void print(String str){
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(logFilePath);
bw = new BufferedWriter(fw);
bw.write(str);
bw.write("\n");
bw.newLine();
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
- 获取文件的大小
FileInputStream is = new FileInputStream(logFile);
int length = is.available();
- 获取Android Cache 目录
public static final String logFilePath = MyApplication.getMyApplicationContext().getCacheDir()+"/myLog.txt";
- 读取文件的方法 并转换成String
private void readFile() {
File file = new File(LogFileUtil.logFilePath);
if (file.exists()) {
try {
FileInputStream fis = new FileInputStream(file);
int length = fis.available();
logFileInfo = "文件路径: \n" + LogFileUtil.logFilePath + "\n";
logFileInfo = logFileInfo + "length: " + length + " byte\n";
byte[] buffer = new byte[length];
fis.read(buffer);
String ss = new String(buffer, "UTF-8");
mTvLogContent.setText(ss);
fis.close();
} catch (IOException e) {
e.printStackTrace();
mTvLogContent.setText("读取日志错误");
}
}
}