Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Line Feed Carriage Return problem

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Do you know how to print a Line feed carriage return character not using "\n"
i am trying to generate a text file (plain) and the "\n" character doesn't work, the charset is UTF-8

this my code



and the unicode characters fails too (doesn't compile).
Thanks in advance!
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't quite understand why you can't use \n (both CR and LF are part of UTF-8), but ".append((char) 10)" and ".append((char) 13)" would append those two characters.
 
Carlos Mendoza
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer Ulf, neither do I.
i try your code and nothing happen!
I'm not understand why the "\n" doesn´t works.
Ok i'll give you more info. i'm trying to generate a plain file and send it as response from an action this is how im try to do that:



the getLayout method returns the string that will be the report.
and the getIs method returns the info as bytes for response to the client (FYI Struts 2 framework).

if i display the response in a browser it works fine.

thanks for your help

P.d this is the right forum right?
 
Carlos Mendoza
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh ok all right!

i change the order of append((char) 10).append((char) 13) to append((char) 13).append((char) 10) and works fine!
thank you Dittmer!
 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll find it a lot more convenient to go back to the original style and write that as
 
Sheriff
Posts: 21950
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:You'll find it a lot more convenient to go back to the original style and write that as


I agree. (char)10 is the same as '\n' and (char)13 is the same as '\r'. Your code does nothing more than append these two characters. Appending "\r\n" will do exactly the same.

Off-topic: I've seen the following code written by a professional. It made me cringe:
Suffice to say I simply replaced it with "\r\n" and I was happy again.
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats wrong with line.separator?
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:Whats wrong with line.separator?



Because the value of the property is platform specific. If you want to guarantee that the generated file will always contain lines that are delimited by the \r\n character sequence the line.separator property will trip you up on certain platforms.
 
Carlos Mendoza
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mmm well i'll try the orignal style
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic