Home arrow static arrow Java Programming [Archive] - '\n' != "line.separator" ?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - '\n' != "line.separator" ?
This topic has 21 replies on 2 pages.    1 | 2 | Next »

Posts:3,369
Registered: 24.10.97
'\n' != "line.separator" ?  
Jun 22, 2004 9:08 AM



 
I wrote my own TextFormatter class for logging. I looked at the java.util.logging.SimpleFormatter to have an idea to overwrite the abstract

public synchronized String format(LogRecord record)

of the Formatter class. It works fine but i have the following strange behaviour: if i get the lineSeparator like in SimpleFormatter:

    private String lineSeparator = (String) java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("line.separator"));


Or via

lineSeparator = System.getProperty("line.separator");


I don't get broken lines in my code. If i use instead "\n" (String with backslash n) it works; i.e. the lines get broken. (I'm running on Windows 2000)

The following test code also shows that line.separator doesn't seem to be "\n":

        lineSeparator = System.getProperty("line.separator");System.out.println("line.separator: '"+lineSeparator+"'; equals: "+(lineSeparator.equals("\n")));         lineSeparator = "\n";System.out.println("backslash n: '"+lineSeparator+"'");


output:

line.separator: '
'; equals: false
backslash n: '
'

Is this a logging API issue?
 

Posts:11,200
Registered: 7/22/99
Re: '\n' != "line.separator" ?  
Jun 22, 2004 9:12 AM (reply 1 of 21)



 
On Microsoft Windows systems the platform's native line separator is \r\n.
 

Posts:37,103
Registered: 3/30/99
Re: '\n' != "line.separator" ?  
Jun 22, 2004 9:15 AM (reply 2 of 21)



 
An on the Mac it's \r
 

Posts:1,183
Registered: 1/23/02
Re: '\n' != "line.separator" ?  
Jun 22, 2004 9:30 AM (reply 3 of 21)



 
Note that Java works some magic when printing to the console, so System.out.print("\n") will start a new line even on Windows and Mac.
 

Posts:11,200
Registered: 7/22/99
Re: '\n' != "line.separator" ?  
Jun 22, 2004 9:37 AM (reply 4 of 21)



 
Note that Java works some magic when printing to the
console, so System.out.print("\n") will start a new
line even on Windows and Mac.

Java does not do any "magic," it writes only what you ask it to write. '\n' will not be converted to \r\n or anything else. You can easily demonstrate this by writing a text file with \n from Java and opening it in Notepad.
 

Posts:37,103
Registered: 3/30/99
Re: '\n' != "line.separator" ?  
Jun 22, 2004 10:03 AM (reply 5 of 21)



 
If you use the line.separator system property, you should always get line breaks. Without seeing more of your code, I can't say why you're not.

The reason you're seeing a line break even when using \n which is not the separator on your system is probably that the terminal software you're running is smart enough to handle various flavors of line separator, even ones that aren't the "official" one for your OS.
 

Posts:1,183
Registered: 1/23/02
Re: '\n' != "line.separator" ?  
Jun 22, 2004 10:07 AM (reply 6 of 21)



 
You can easily demonstrate this by writing a text file with \n from Java and opening it in Notepad.

Me wrong, you right. It's in fact the Windows console that generously allows '\n' to have the effect of CR/LF. Which makes me worried about the strength of my assertion that System.out.print("\n") will start a new line on a Mac console. Will it?
 

Posts:37,103
Registered: 3/30/99
Re: '\n' != "line.separator" ?  
Jun 22, 2004 10:23 AM (reply 7 of 21)



 
You can easily demonstrate this by writing a text
file with \n from Java and opening it in Notepad.

Me wrong, you right. It's in fact the Windows console
that generously allows '\n' to have the effect of
CR/LF. Which makes me worried about the strength of
my assertion that System.out.print("\n") will start a
new line on a Mac console. Will it?

I'm almost certain it does. However if you were to write that to a file and open it with, say, TextEdit (is that what Mac's equivalent of Notepad is called?) you might not see the line break.
 

Posts:1,183
Registered: 1/23/02
Re: '\n' != "line.separator" ?  
Jun 22, 2004 10:30 AM (reply 8 of 21)



 
However if you were to write that to a file and open it with, say, TextEdit (is that what Mac's equivalent of Notepad is called?) you might not see the line break.

Yep, that's how I convinced myself that you and jsalonen were right in spite of my deep-rooted, misplaced beliefs. I used a shell redirect to write the console output to the file.
 

Posts:11,200
Registered: 7/22/99
Re: '\n' != "line.separator" ?  
Jun 22, 2004 10:47 AM (reply 9 of 21)



 
MacOS traditionally does not have a "console." This has changed in OS X, but I am not sure whether the line separator on OS X is \r or \n since it's also a *NIX system.
 

Posts:551
Registered: 00-08-30
Re: '\n' != "line.separator" ?  
Jun 22, 2004 10:58 AM (reply 10 of 21)



 
WIN ... Carriage Return & Line Feed (\r\n)
MAC ... Carriage Return(\r)
UNIX ... Line Feed (\n)

 

Posts:37,103
Registered: 3/30/99
Re: '\n' != "line.separator" ?  
Jun 22, 2004 10:59 AM (reply 11 of 21)



 
WIN ... Carriage Return & Line Feed (\r\n)
MAC ... Carriage Return(\r)
UNIX ... Line Feed (\n)

Yes, that's been established already.
 

Posts:3,183
Registered: 10.09.03
Re: '\n' != "line.separator" ?  
Jun 22, 2004 12:03 PM (reply 12 of 21)



 
I thought the PrintStream converted newlines to native OS format.
 

Posts:37,103
Registered: 3/30/99
Re: '\n' != "line.separator" ?  
Jun 22, 2004 12:07 PM (reply 13 of 21)



 
I thought the PrintStream converted newlines to native
OS format.

[url http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintStream.html#println()]It does[/url]
 

Posts:1,183
Registered: 1/23/02
Re: '\n' != "line.separator" ?  
Jun 22, 2004 12:15 PM (reply 14 of 21)



 
[url http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintSt eam.html#println()]It does[/url]

?

It says is that it'll add an OS-native newline after the output. Nothing about conversion of "\n" in the output itself.
 
This topic has 21 replies on 2 pages.    1 | 2 | Next »