1

I am facing an issue while reading a file with special characters. My .txt file has data:

I am reading this file using following code:

StringBuilder sBuilderString = new StringBuilder();

for (int n; (n = loInputStream.read()) != -1;) {
    sBuilderString.append((char)n);
}

This string is now again used to write a file, the issue is that when i write the file, one of these two characters is replaced by some other special character.

How can i write code, which is able to read all the special characters and write that to another file?

Mark Bell
  • 28,985
  • 26
  • 118
  • 145
Ankit
  • 3,083
  • 7
  • 35
  • 59

2 Answers2

2

You have issues with the encoding of your characters. The call to '(char) n) will effectively transform byte n into a character using the default character encoding of your system, which might differ from the encoding of your source file.

One way to avoid that is to wrap your InputStream in a CharacterInputStream, where you can specify the character encoding:

Reader reader = new InputStreamReader( loInputStream, "UTF-8");

You can then proceed to read your stream into your StringBuilder. I would also recommend to wrap your reader with a bufferedReader to improve performance with blocking IO streams.

Reader reader = new BufferedReader(new InputStreamReader( loInputStream, "UTF-8"));
maasg
  • 37,100
  • 11
  • 88
  • 115
  • @maasg: How can i get encoding format of my file (I am trying to read image stream, .jpg file) – Ankit Jun 26 '11 at 09:06
  • 1
    @Ankit: that is binary data, not text, so don't treat it as text. It doesn't make sense to put it on a `StringBuilder`. – ninjalj Jun 26 '11 at 09:12
  • @Ankit as @ninjalj said, don't treat binary data as chars. You need to preserve is as bytes all the way. Look into nio.ByteBuffer – maasg Jun 26 '11 at 09:17
  • @Ankit btw, your question "My .txt file has data..." didn't hint to the jpg format. Something to keep in mind next time. – maasg Jun 26 '11 at 09:54
  • @maasg & @ninjalj : Thanks for your effort, the reason why I want to read the stream as String is that I want to send this stream String in post request for REST, where it can be recreated as image again. I can not directly accept the stream for recreation of image. – Ankit Jun 26 '11 at 10:31
  • @maasg: I wrote text file had this special characters because i didn't wanted to make the question complex. Anyways, will keep in mind from next time. – Ankit Jun 26 '11 at 10:32
  • @Ankit: for xfer of binary as text, look for Base64 encoding. – maasg Jun 26 '11 at 11:01
  • @maasg: Can you please elaborate.. Thanks – Ankit Jun 26 '11 at 11:42
  • @Ankit: Base64 is typically used to convert binary formats into printable characters for text-based transfer. A common use are MIME attachments to email, etc. Apache has an impl here: http://goo.gl/LTnQv Don't know yr requirements to extends this more. Ask another question w/more details if you want. Comments are more for clarifications on current question. – maasg Jun 26 '11 at 13:02
1

Use InputStreamReader and specify encoding which is used in the file.

Jarek Potiuk
  • 19,317
  • 2
  • 60
  • 61