Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Maintaining New Line and Tab Delimiters when writing String to File  RSS feed

 
Andrew Bradford
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following string is the result of editing an XML file using ACE JavaScript Editor. I want to be able to preserve the format when the new file is written. My problem is that I am only able to write a single line file even though the string contains Tabs [\t] and New Line [\r\n]. Both PrintWriter and BufferedStreams result in a single line. What am I missing here as I am assuming I should be able to maintain the formatting contained within the string?


 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Can you show us the code which writes to the file?
I ran this small snippet


And got this output correctly formatted
Screen-Shot-2016-10-18-at-3.34.45-PM.png
[Thumbnail for Screen-Shot-2016-10-18-at-3.34.45-PM.png]
 
Andrew Bradford
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so this is a little strange. Yes, you have posted exactly what I would have expected the result to be. So lets just expand on what I am seeing. I have JSP with some JS which sends the String via an Ajax call to a Servlet which tries to write the file. So, I've simplified the previous example using Pseudo code...

JavaScript:




In the Servlet,



The result is really odd..... I get the following in the output file:

ABC
CD
AB\nCD <--- This is the Argument Sent Through Ajax

When I Print the [fileContent] to the Console before I print to file it looks correct [AB\nCD]. Can't see anything strange going on there but there clearly is!! Argghhh pulling hair out with this one.. Is there a particular way that I can check for special characters?


 
Dave Tolls
Rancher
Posts: 2909
35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You are escaping the slash, so that is treated from then on as an actual slash and not as an escape character.
 
Andrew Bradford
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dave... Must be getting tired! In addition, I've Encoded the FileContent String in the JS using,



I think we're finally there!

Thanks mate. Appreciated.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!