Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Internationalization: char to byte String.getBytes() conversion error

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Internationalization: char to byte String.getBytes() conversion error

    Hi Team,

    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,
    Khyana


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Internationalization: char to byte String.getBytes() conversion error

    Quote Originally Posted by khyana View Post
    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.
    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.

    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.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. String.split"char"; error
    By bean in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 3rd, 2013, 05:30 PM
  2. Replies: 3
    Last Post: March 23rd, 2013, 07:20 PM
  3. Get int value from char, char pulled from String
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 10:04 AM
  4. RSA Decryption with Java.security - Hex to dec to byte array conversion...
    By SeanSeanston in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 15th, 2010, 09:48 AM
  5. [SOLVED] utf-16 byte[] to string conversion
    By Gerhardl in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 25th, 2010, 07:06 AM