4
我正在嘗試向我的應用程序的日誌寫入文件。我創建了一個將行添加到行尾的方法。寫入文件的新行
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()
,但我一直讓所有在短短的一行:(
任何想法
logWriter.newLine()爲我工作。 BufferedWriter的newLine()也可以工作。很奇怪 – Reno 2011-02-08 11:37:05