写入文件的新行
我正在尝试将应用程序的日志写入文件。我创建了一个方法,将一行附加到行尾。
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()
但我总是在一行中获取所有内容:(
有什么想法吗?
I am trying to write to a file the log of my application. I have created an method which appends a line to the end of the line.
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());
}
}
}
Everything works fine less the new character line.
I have tried using:
\r\n
System.getProperty("line.separator")
newline()
But i keep getting all in just one line :(
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this:
尝试这个
TimeReporter 类和函数
Try this
TimeReporter class and function