-4

I have a string which is decoded/encoded/encrypted through a long process (file writing/reading involved).

On the beginning of my "clear-text" String after decoding I have two strange characters : ��

What is this? Why is it there? How to get rid of it?

Thanks.

edit:

This is how I write a file:

try {
   FileOutputStream out = new FileOutputStream(filePath);
   out.write(string.getBytes());
   out.close();
} catch (Exception e) {
   //handle exception
}
user5581557
  • 205
  • 3
  • 13
  • I think this is a Byte-Order Mark (BOM) http://stackoverflow.com/questions/2223882/whats-different-between-utf-8-and-utf-8-without-bom – Stewart May 25 '16 at 10:37
  • 2
    It's probably a [BOM](https://en.wikipedia.org/wiki/Byte_order_mark), that was created somewhere in your file writing. Show your code. – Kayaman May 25 '16 at 10:37
  • Make sure to read this before asking: http://stackoverflow.com/help/how-to-ask – tak3shi May 25 '16 at 10:37
  • 1
    If you down vote, can I ask for a reason? Constructive criticism would improve my questions more than getting minuses. – user5581557 May 25 '16 at 10:40
  • Probably because two `??` characters is simply what the browser prints for an unknown glyph. Without knowing (a) the exact byte eg, `3F` (b) the encoding, eg, `UTF-8`, then nobody can know what those characters are. – Stewart May 25 '16 at 10:43
  • 1
    @user5581557 `string.getBytes()` => specify an encoding in there and use the same encoding to read the file back. – assylias May 25 '16 at 10:44

1 Answers1

1

I can pretty much guarantee it's an encoding issue

  1. Never use new String(byte[]) (always pass a charset)
  2. Never use new InputStreamReader(inputStream) (always pass a charset)
  3. Never use String.getBytes() (always pass a charset)
  4. Make sure the editor (eg notepad) is reading using the same encoding as the file was written

If you don't explicitly pass a charset, a default will be chosen. In java this will be different for UNIX/Windows (unless you explicitly set the file.encoding system property).

lance-java
  • 21,832
  • 3
  • 45
  • 81