Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

notepad doesn't recognize \n character?

Tags:

java

file

css

I am copying some css classes to one file. Classes get copied very well, but I have a problem that when I am trying to open it using notepad it gives one square instead of \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 this format() function

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 show up properly in notepad?

like image 263
dhananjay Avatar asked Mar 14 '12 11:03

dhananjay


People also ask

How do you show newline characters on notepad?

Open any text file and click on the pilcrow (¶) button. Notepad++ will show all of the characters with newline characters in either the CR and LF format. If it is a Windows EOL encoded file, the newline characters of CR LF will appear (\r\n). If the file is UNIX or Mac EOL encoded, then it will only show LF (\n).

Is it n or \n for new line?

Operating systems have special characters denoting the start of a new line. For example, in Linux a new line is denoted by “\n”, also called a Line Feed. In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF.

What is \n line break?

With early computers, an ASCII code was created to represent a new line because all text was on one line. In programming languages, such as C, Java, and Perl, the newline character is represented as a '\n' which is an escape sequence. Below are examples of how the newline could be used in Perl.


2 Answers

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

public static String newLine = System.getProperty("line.separator");
//...
out.write(newLine);
like image 89
MByD Avatar answered Oct 19 '22 18:10

MByD


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

out.write(main);
out.newLine();
like image 31
hmjd Avatar answered Oct 19 '22 20:10

hmjd