New line entry to file

I am trying to write the log of my application to a file. I created a method that adds a line to the end of a 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 than a new line of characters.

I tried using:

 \r\n System.getProperty("line.separator") newline() 

But I keep getting everything on one line :(

Any ideas?

+4
source share
2 answers

Try the following:

 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(); 
+2
source

try it

 public static boolean writeLog(String user , Exception exS) { boolean d = false; try { File root = new File("TVS_log"); if (!root.exists()) { root.mkdirs(); } String name = user + "_" + TimeReporter.getTodayAll() + "_" + "log.txt" ; System.out.println(name); File text = new File(root , name); if (!text.exists()) { text.createNewFile(); } String aggregate = ""; for (StackTraceElement element : exS.getStackTrace()) { BufferedWriter writer = new BufferedWriter(new FileWriter(text)) ; String message = exS.toString() + " - " + element.toString() + "\r\n" ; System.out.println(exS.toString()); System.out.println(element.toString()); aggregate += message; writer.write (aggregate); writer.newLine(); writer.flush(); writer.close(); writer = null; } if(text.exists()) return text.length() > 0; }catch(Exception ex){ ex.printStackTrace(); } return d; } 

Class and function TimeReporter

 public class TimeReporter { public static String getTodaysDate() { String d = ""; final Calendar c = Calendar.getInstance(); d = String.format("%02d", c.get(Calendar.YEAR)) + String.format("%02d", c.get(Calendar.MONTH) + 1) + String.format("%02d", c.get(Calendar.DAY_OF_MONTH)); return d; } public static String getTodaysTime() { String d = ""; final Calendar c = Calendar.getInstance(); d = String.format("%02d", c.get(Calendar.HOUR_OF_DAY)) + String.format("%02d", c.get(Calendar.MINUTE)) + String.format("%02d", c.get(Calendar.SECOND)); return d; } public static String getTodayAll() { return getTodaysDate() + "_" + getTodaysTime() ; } public static String getNow() { final Calendar c = Calendar.getInstance(); String ld = c.get(Calendar.YEAR) + "/" + (c.get(Calendar.MONTH) + 1) + "/" + c.get(Calendar.DAY_OF_MONTH) + " " + String.format("%02d", c.get(Calendar.HOUR_OF_DAY)) + ":" + String.format("%02d", c.get(Calendar.MINUTE)); return ld; } } 
0
source

Source: https://habr.com/ru/post/1338833/


All Articles