我试图写一个文件我的应用程序的日志.我创建了一个方法,在行的末尾添加一行.
public static void write2Log(String s){
StringBuilder contents = new StringBuilder();
File root = Environment.getExternalStorageDirectory();
File logFile = new File(root, "testWrite2file.log");
if (root.canWrite()){
try {
BufferedReader input = new BufferedReader(new FileReader(logFile));
String line = null;
while (( line = input.readLine()) != null){
contents.append(line);
// contents.append(System.getProperty("line.separator"));
}
input.close();
FileWriter logWriter = new FileWriter(logFile);
BufferedWriter out = new BufferedWriter(logWriter);
out.write(contents.toString()+"\r\n"+s);////<---HERE IS MY QUESTION
out.close();
}
catch (IOException e) {
Log.e("test", "Could not read/write file " + e.getMessage());
}
}
}
一切都很好,而不是新的角色线.
我尝试过使用:
\r\n
System.getProperty("line.separator")
newline()
但我一直只有一行:(
有任何想法吗?
最佳答案 试试这个:
FileWriter logWriter = new FileWriter(logFile);
BufferedWriter out = new BufferedWriter(logWriter);
out.write(contents.toString());////<---HERE IS THE CHANGE
out.newLine();
out.write(s);
out.close();