This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with newlines in text files  RSS feed

 
Tomas Nilson
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey!

I am generating a txt file from a JSP. In the beginning of the file I have

response.setContentType("text/plain");
response.setHeader("Content-Disposition","attachment;filename=test.txt")


and then I use out.print to write to the file. The problem is that newlines come out as a rectangle when I view the file in Notepad. If I copy the rectangle and try a paste it in this area here it appears as a newline.... Does anyone know what is going on? How can I solve this?

Thanks a bunch, Tom
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JSP engine will faithfully reproduce any whitespace that is in the JSP source file, including newlines after directives and other elements that do not themselves produce output.

It might be instructive to take a gander at the Java file created on behalf of your JSP by the engine. You will see lots of out.print()'s for the newlines.

There's not much you can do about it without either resorting to funky formatting in your original JSP, or utilizing a Filter to perform post-processing on the response to remove the unwanted whitespace (assuming you can come up with a deterministic algorithm to determine what's wanted vs. unwanted).
 
Tomas Nilson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply! The problem is that the newlines don't appear as newlines in my generated file, instead they appear as those rectangles that I was talking about. So I want newlines but I can't get them.. Any ideas?

//Tom
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Odd, that usually means unprintable characters in the output. I'd suggest looking at the Java file as I mentioned earlier -- that way you can see exactly what is being emitted.
[ October 22, 2004: Message edited by: Bear Bibeault ]
 
Tomas Nilson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked the generated servlet and it is a line like:

out.print(string + "\n"); where string is a String

That causes these rectangles... Any ideas??

Thanks, Tom
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All I can guess is that Notepad is boofing on the newlines. Does the file look ok in the browser? Other text editors?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you might want to try \r or \c

doubt it will work, but who knows...
 
Pedro Gongora
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i think that is because on windows the new line character are really two characters: "\r\n", and you are appending only "\n".
 
Tomas Nilson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much guys! The \r\n worked! Silly of me not to add the "no Rectangle" in the first place...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!