This discussion is archived
2 Replies Latest reply: May 22, 2013 4:45 PM by rp0428 RSS

Problem with encoding and charset for downloading a file

800405 Newbie
Currently Being Moderated
Hi guys, I have a problem and I beg for your help, I am 1000% frustrated at this point.

I have a servlet which have to do something and give a file log to the final user. Coding the logic for that took me about 5 minutes, the problem is that the given file doesnt shows properly in notepad (Default app to open txt files). I have tried every way I have read over the internet and absolutely nothing works.

After trying about 20 different ways fo doing this without success, this is my actual code:

Charset def=Charset.defaultCharset();
OutputStreamWriter out = new OutputStreamWriter(servletOutputStream,def);
for (String registry:regList) {
out.write(registry+"\n");
}
out.close();

the page gives the file to the user, I can download or open it, when I open it this is the result

registry1registry2registry3registry4registry5registry6registry7...

and I am expecting:

registry1
registry2
registry3
registry4
registry5
.
.
.


If I open it with wordpad or notepad++ the file looks fine, but I cant achieve that notepad reads it correctly. I have spent about 10 hours on this and at this point I just dont know what to do, i have tried Windows-1252, UTF-8, UTF-16, the Default one. I have tried to set this enconding on the response header with no luck. Any help will be very appreciated.

Thanks in advance.
  • 1. Re: Problem with encoding and charset for downloading a file
    jtahlborn Expert
    Currently Being Moderated
    that has nothing to do with character sets and everything to do with windows newline conventions. the newline convention on windows is "\r\n", and notepad only understands this format.
  • 2. Re: Problem with encoding and charset for downloading a file
    rp0428 Guru
    Currently Being Moderated
    >
    I have a servlet which have to do something and give a file log to the final user. Coding the logic for that took me about 5 minutes, the problem is that the given file doesnt shows properly in notepad (Default app to open txt files). I have tried every way I have read over the internet and absolutely nothing works.
    . . .
    If I open it with wordpad or notepad++ the file looks fine, but I cant achieve that notepad reads it correctly. I have spent about 10 hours on this and at this point I just dont know what to do, i have tried Windows-1252, UTF-8, UTF-16, the Default one. I have tried to set this enconding on the response header with no luck. Any help will be very appreciated.
    >
    Your file likely uses *nix style line endings and use a single LF (0x0A) as the end of each line.

    Notepad doesn't recognize a single LF as the end of line; it expects CRLF (0x0D0A). The encoding isn't the issue.

    If you have to use Notepad you will need to add code to find all of the LF characters and insert a CR character in front of them.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points