Notepad does not recognize \ n?

I copy some CSS classes to a single file. Classes copy very well, but I have a problem that when I try to open it with notepad, it gives one square instead of the \n character. It opens well in Edit +. Here is my code:

 String fileName = new File(oldFileName).getName(); BufferedWriter out = null; FileWriter fw = new FileWriter("D:\\temp\\UPDATED_"+fileName); out = new BufferedWriter(fw); for (CSSStyleRule p : finlist.values()) { String t = null; String m = p.toString(); if (m.charAt(0) == '*') { t = m.substring(1); } else { t = m; } String main = format(t); out.write(main); out.write("\n"); } 

also see function format ()

 private static String format(String input) { int s = input.indexOf('{'); int p = input.indexOf('}'); int w = input.indexOf(';'); if(w==-1) { w=p-1; String []part=input.split("}"); input= part[0].concat(";").concat("}"); } String m = input.substring(0, s).trim().concat("{\n") .concat(input.substring(s + 1, w + 1).trim()) .concat(input.substring(w + 1, p)); String a[] = m.split(";"); String main = ""; for (String part : a) { if (part.contains("rgb")) { part = convert(part); } if(part.contains("FONT-FAMILY") || part.contains("font-family")){ part=process(part); } main = main.concat(part.trim().concat(";")).concat("\n"); } main = main.concat("}"); return main; } 

How to make it display correctly in notepad?

+6
source share
2 answers

Windows uses \r\n for a new line. Use the line.separator property line.separator :

 public static String newLine = System.getProperty("line.separator"); //... out.write(newLine); 
+16
source

Use System.getProperty("line.separator"); , not hardcoded "\n" , since the line separator in the windows is "\r\n" or, in this case, use the BufferedWriter newLine() method:

 out.write(main); out.newLine(); 
+4
source

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


All Articles