2012-03-14 20 views
6

Estoy copiando algunas clases de CSS en un archivo. Las clases se copian muy bien, pero tengo un problema que cuando intento abrirlo usando el bloc de notas, me da un cuadrado en lugar de \n caracteres. Se abre bien en Editar +. Aquí está mi código:Bloc de notas no reconoce el carácter n?

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"); 
} 

también ver este formato de la función()

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; 

    } 

Cómo hacer que se muestre correctamente en el bloc de notas?

Respuesta

16

Windows usa \r\n para la nueva línea. Utilice la propiedad line.separator lugar:

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

gracias por su ayuda – dhananjay

4

Uso System.getProperty("line.separator");, no hardcoded "\n", como separador de línea en las ventanas es "\r\n" o, en este caso, el uso BufferedWriter 's newLine() método:

out.write(main); 
out.newLine(); 
+0

si considera código como este no es útil main = main.concat (part.trim(). Concat (";")). Concat ("\ n"); – dhananjay

+0

Uso: 'main = main.concat (part.trim(). Concat ("; ")). Concat (System.getProperty (" line.separator "));'? – hmjd

+0

line.separator debe pasar en comillas dobles ?? – dhananjay

Cuestiones relacionadas