This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes newline characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "newline characters" Watch "newline characters" New topic
Author

newline characters

James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
We have a servlet that takes a text file, appends a couple of lines to it, then returns the modified text file to the client.

The problem is that when I test the servlet, the new lines in the text file are not appearing as new lines. Instead of getting:

original line 1
original line 2
original line 3
added-by-servlet line 1
added-by-servlet line 2

we get:

original line 1
original line 2
original line 3
added-by-servlet line 1#added-by-servlet line 2#

(where # is actually the square (unrecognisable character) symbol)

We have tried using "\n" and (char)10 to as the newline character in the servlet, but they both show up at the client as unrecognisable characters.

This problem occurs when using MS Notepad to view the returned text file, and also seems to occur when a Motorola phone is doing the request.

Does anyone know what could be going wrong?

Thanks in advance,
James
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
You need to adapt your linebreaks according to your intended platform. Windows uses "\r\n" (i.e. 13 and 10), Unix just "\n" (and Mac OS 9 and earlier just "\r", for the terminally curious). There's also a system property called line.separator that you can use for this, assuming your generating and consuming platforms are identical.


Ping & DNS - my free Android networking tools app
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Most importantly: Notepad, unlike many Windows programs, is incapable of properly displaying text files with UNIX line endings. For notepad to read a file, it must contain line endings consisting of the two characters 0x13, 0x10 (or ^M^J, or \r\n, or however you like to write it). Nothing else will do.


[Jess in Action][AskingGoodQuestions]
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
Thanks for the reply, Ulf.

I'd already tried \n and \r. I gave System.getProperty("line.separator") a go and I got exactly the same result.

The odd thing is that the unrecognisable character is code 10 (I checked it in MS Excel). But the good line breaks (e.g., like after "original line 2") are also code 10.

So how come my code 10's are not good enough?!

Also, the server and client platforms are not identical, I think the servlet is sitting on a Unix server. I am Windows, and I've not idea about the Motorola... But it seems this is definately something to do with it.

Cheers,
James
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

It's \r \n, not \n \r. Makes a difference! Also, the line.separator property gives the separator appropriate for the platform the program is on -- in this case, a UNIX server, not the Windows client. Seriously, do what I said: use the two characters "\r \n", in that order.
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
Cheers Sheriff,

Never heard of "\r\n" - I just presumed it was a typo!...

Hey, but it works! Thanks both of you.

Regards,
James
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
What is the newline character?
http://qa.jtiger.org/GetQAndA.action?qids=62&showAnswers=true


Tony Morris
Java Q&A (FAQ, Trivia)
 
 
subject: newline characters