Internationalization: char to byte String.getBytes() conversion error
Greetings for the day!!!!
This is my first post in this forum. Kindly support me with valuable replies.
Issue: Here i am trying to write a byte to my fileOutput stream in a java file.
code line: objSmbFileOutputStream.write(strContent.getBytes() );
When I am running the code,its giving me error atating :Internationalization: char to byte String.getBytes() conversion using the runtime default encoding.
Explicitly specify one of UTF-8, UTF-16, US-ASCII, ISO-8859-1, UTF-16BE, UTF-16LE encodings instead.
Can anyone help me in resolving the issue . Do let me know how can I change the char to byte using UTF 16 standard.
Many thanks in advance,
Re: Internationalization: char to byte String.getBytes() conversion error
Firstly, when there is an encoding/decoding of a String into/from bytes, the charset is always an important matter. It can be implicit (the default charset of the platform) or explicit, but you always need to care about this.
Originally Posted by khyana
Secondly, if you are using a (File)OutputStream because you are writing binary/mixed datas, it's ok to write a String as a sequence of bytes (depending on the format of the file you are writing, this is up to you to know if it has sense). In this case, use the other getBytes(String charsetName) method.
If you are writing a text-only stream, please wrap the (File)OutputStream in a OutputStreamWriter, so you can write at least "characters" and not simple bytes. And eventually use also another wrapper e.g. BufferedWriter. And please see the constructors of OutputStreamWriter to decide if you want to use a default charset or an explicit charset.
If you have doubts, explain what type of file you are writing.