Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

newline characters

 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic