This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

UNIX line break problem  RSS feed

 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to make a comma separated file, but I experience problems with line breaks, when opening the file with Notepad or reading the file into the financial system.
The web server, where the file is created, is running on UNIX, and I need to be able to open the file on a Windows platform.

When I open the file with Notepad I see a rectangle, instead of a line break. It works fine when opening the file using Excel or Wordpad.

I have tried to use the code below:


I have tried to replace System.getProperty("line.separator") with "\n" and "\n\r" but it doesn´t solve the problem.

Is it possible to solve the problem from the Java code?

 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want "\r\n" not "\n\r". That's the standard DOS line ending, and as you've discovered, Notepad is really stupid about that. Wordpad and most other text editors are smart enough to recognize different line endings.
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:You want "\r\n" not "\n\r". That's the standard DOS line ending, and as you've discovered, Notepad is really stupid about that. Wordpad and most other text editors are smart enough to recognize different line endings.


Alright I tried with "\r\n" as well as "\n".

When opening the file using Nodepad it shows:
A1;A2\r\nB1;B2

So I guess there is no other ways to work around this issue?
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using String "\r\n" and not "\\r\\n"? You should use the former.
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Are you using String "\r\n" and not "\\r\\n"? You should use the former.


Yes, I have tried both:

- when using "\r\n", the result is:
A1;A2\r\nB1;B2

- when using "\\r\\n", the result is:
A1;A2\\r\\nB1;B2

I get the line break code from a XML file:
<lineBreak>\r\n</lineBreak>

I build the file using a StringBuffer:


- And I write it to HttpServletResponse from a servlet using:


Then I save the file on my desktop and open it using Notepad.
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, my mistake!

It works now when escaped to:

"& #13 ; & #10 ; "


Thanks for your help :-)
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeppe Sommer wrote:I get the line break code from a XML file:
<lineBreak>\r\n</lineBreak>

That means you are using String literal "\\r\\n", not "\r\n". Your solution ensures that the actual characters \r and \n are used.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!