• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

UNIX line break problem

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, my mistake!

It works now when escaped to:

"& #13 ; & #10 ; "


Thanks for your help :-)
 
Rob Spoor
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic