• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stringbuffer and carriage return  RSS feed

 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to read a file and then save all of its content in one stringbuffer so that I will read the file just once.



and then using the stringbuffer, i will output the it to another file using this code.



But when I open the output file using Windows Notepad, it displays in a single line and not the original format of the file.
I tried changing this line


to


But it still displays the same single line of text.

Have tried also, opening using wordpad but it interprets the other character as boxes.

Is there something that I need to add or change? Thanks.

 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Might be an encoding problem, but this sounds too difficult for use beginners. Moving.

I know Notepad can't cope with unfamiliar line ends: most applications "know" that line end is \n on *nix, \r on Mac (older versions) and \r\n on DOS/Windows, but not Notepad.
Why are you using StringBuffer, which is intended for altering Strings, rather than List<String> since you appear to be simply storing the Strings?
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mr Campbell,

Thanks for your reply.
Why are you using StringBuffer, which is intended for altering Strings, rather than List<String> since you appear to be simply storing the Strings?


I am trying to read the content of the whole file in one open-read-close sequence and store the content in one stringbuffer.
I think stringbuffer is much suited for string concatenation/alteration so I did it this way.

I am just not sure why Notepad gets interpreted this way.

Hope someone can help explain this to me. Thanks..
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried changing the encoding of your output, remember that Java strings are UTF-16... (I'm not saying this _is_ your problem, but it's something worth trying).
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Martijn,

Thanks for the reply.
Have you tried changing the encoding of your output, remember that Java strings are UTF-16... (I'm not saying this _is_ your problem, but it's something worth trying


I tried what you have suggested and change the encoding scheme as follows.

From:


To:


When I open the text file, the content are now organized according to the original file with carriage return.

But my problem is the spaces between characters are very big and the text is hard to read. Probably because
of the encoding that I applied. I have tried also the UTF-8 encoding scheme but the results are the same.

I think I need to change my algorithm. Can you suggest what can I do to achieve what I want?

 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

It would be fairly unusual to write files out deliberately as UTF-16. Now notepad is a fairly simplistic text editor that can only support certain encodings, so you need to:

Definitely read this article by Joel on Software

After reading that article you'll probably realise that you need to either:

Write out in an encoding that Notepad can handle OR open the file in a text editor that can handle that encoding.

Hope that helps!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!