我正在尝试将日志数据写入android中的文件,然后再次将文件读入字符串。不幸的是我在阅读文件时遇到错误。
我无法理解存储文件是否存在问题,我也想知道我在读取内容时从android获取文件时是否犯了错误。 我在阅读文件内容时遇到错误。请让我知道如何解决从android文件读取的问题。
当我使用以下行检查我的文件存储位置时:
android.util.Log.d("myapp",logFile.toString());
我有以下位置:
/storage/emulator/0/yourLog.txt
我正在使用以下代码将日志数据写入文件。
public File writeLogDataToFile(String LogData){
File logFile = null;
try {
logFile = new File(Environment.getExternalStorageDirectory(),
"yourLog.txt");
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile,
true));
buf.append(LogData);
buf.newLine();
buf.close();
android.util.Log.d("myapp",logFile.toString());
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return logFile;
}
以下是用于将文件内容读入String的代码:
/* Step no 4 : Get the Contents of the File */
private String getContentsOfFile() throws FileNotFoundException {
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
//File file = new File("sdcard/yourLog.txt");
File file = new File(Environment.getExternalStorageDirectory(),
"yourLog.txt");
android.util.Log.d("myapp",file.toString());
//Read text from file
StringBuilder text = new StringBuilder();
String line=null;
try {
BufferedReader br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
/*
String content = new Scanner(new File(tempFile)).useDelimiter("\\Z").next();
System.out.println(content);
*/
return line;
}
堆栈追踪:
01-31 15:31:18.695: I/System.out(20748): Step no 3 : Writing the JSONObject to File
01-31 15:31:18.695: I/System.out(20748): Printing the Contents in console wrote to File
01-31 15:31:18.705: D/myapp(20748): /storage/emulated/0/yourLog.txt
01-31 15:31:18.705: I/System.out(20748): Step no 4 : Get the contents of the file
01-31 15:31:18.705: D/myapp(20748): /storage/emulated/0/yourLog.txt
01-31 15:31:18.705: D/AndroidRuntime(20748): Shutting down VM
01-31 15:31:18.705: W/dalvikvm(20748): threadid=1: thread exiting with uncaught exception (group=0x41884ba8)
01-31 15:31:18.705: E/AndroidRuntime(20748): FATAL EXCEPTION: main
01-31 15:31:18.705: E/AndroidRuntime(20748): FATAL EXCEPTION: main
01-31 15:31:18.705: E/AndroidRuntime(20748): Process: com.example.sampleaudit, PID: 20748
01-31 15:31:18.705: E/AndroidRuntime(20748): java.lang.NullPointerException
01-31 15:31:18.705: E/AndroidRuntime(20748): at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
01-31 15:31:18.705: E/AndroidRuntime(20748): at com.example.auditor.LogMessages.getContentsOfFile(LogMessages.java:235)
01-31 15:31:18.705: E/AndroidRuntime(20748): at com.example.auditor.LogMessages.createLogMessages(LogMessages.java:78)
01-31 15:31:18.705: E/AndroidRuntime(20748): at com.example.sampleaudit.MainActivity$2.onClick(MainActivity.java:111)
01-31 15:31:18.705: E/AndroidRuntime(20748): at android.view.View.performClick(View.java:4438)
01-31 15:31:18.705: E/AndroidRuntime(20748): at android.view.View$PerformClick.run(View.java:18422)
01-31 15:31:18.705: E/AndroidRuntime(20748): at android.os.Handler.handleCallback(Handler.java:733)
01-31 15:31:18.705: E/AndroidRuntime(20748): at android.os.Handler.dispatchMessage(Handler.java:95)
01-31 15:31:18.705: E/AndroidRuntime(20748): at android.os.Looper.loop(Looper.java:136)
01-31 15:31:18.705: E/AndroidRuntime(20748): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-31 15:31:18.705: E/AndroidRuntime(20748): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 15:31:18.705: E/AndroidRuntime(20748): at java.lang.reflect.Method.invoke(Method.java:515)
01-31 15:31:18.705: E/AndroidRuntime(20748): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-31 15:31:18.705: E/AndroidRuntime(20748): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-31 15:31:18.705: E/AndroidRuntime(20748): at dalvik.system.NativeStart.main(Native Method)
01-31 15:31:20.195: I/Process(20748): Sending signal. PID: 20748 SIG: 9
答案 0 :(得分:1)
将日志写入Android中的文件
public void writeJsonToFile(String tempJsonString){
File logFile = null;
try {
logFile = new File(Environment.getExternalStorageDirectory(),
"yourLog.txt");
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile,
true));
buf.append(tempJsonString);
buf.newLine();
buf.close();
android.util.Log.d("myapp",logFile.toString());
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
// return logFile;
}
从Android中的文件中读取内容:
private void getContentsOfFile() throws FileNotFoundException {
File file = new File(Environment.getExternalStorageDirectory(),
"yourLog.txt");
android.util.Log.d("myapp",file.toString());
String output = new Scanner(file).useDelimiter("\\Z").next();
System.out.println("" + output);
}