Linebreaks in Java

Aeren

n00b
Joined
Aug 29, 2005
Messages
37
Hi everyone!
I would like to know if i use the following code, how can i decide if the char i read in is the linebreak or not.

while( ( c = in.read( ) ) != -1 ) {
char ch = (char) c;
...

Anyone knows?

Thanks!
 
Depends on OS. Windows actually uses two characters for a line break, CR+LF, and most *nixes use just LF.
BTW, CR = Carriage Return, LF = Line Feed
I haven't done any java programming as of late, you might look in the docs for something about finding "end of line" since the JVM will probably have what character sequence for your OS and be able to check it for you. BTW just because its common for an OS to use one style, doesn't mean every text file will, I know wordpad can edit files with just LF for end of line just fine, but notepad doesn't like them at all.
 
String linebreak = System.getProperty("line.separator");

In windows the linebreak is two characters ("\r\n"), so you will have to add logic to handle that.
 
generelz said:
String linebreak = System.getProperty("line.separator");

In windows the linebreak is two characters ("\r\n"), so you will have to add logic to handle that.

so you're saying, if i read in "\r" then i should check the next char if it's "\n"?
Btw, now that you mention it, isn't linebreak "\n"only?
 
Aeren said:
so you're saying, if i read in "\r" then i should check the next char if it's "\n"?
Btw, now that you mention it, isn't linebreak "\n"only?
As I said, its dependent on whats common for the OS, Windows is normally \r\n, *nix tends to be \n. Its more of just whats common practice then a requirement.
linebreak is what the OS and its developers define
\l = Linefeed character
\r = Carrige Return character
 
To asnwer the original poster, the preffered way to handle such a situation is to go all the way by changing your InputStream into a BufferedReader. Simply use the readLine function and you can be assured nothing wicked will ever happen.
 
Back
Top