Picture the following scenario. A server (running on Windows) sends an object to a client (running on Linux), over a socket connection. This object contains, among other things, a search path in the form of a string. Now, at some point the client takes this string and transforms it into bytes with the getBytes()-method and sends the byte-array back to the server. The server recives this and creates the string again with new String(byte). Here arise a problem, some of the characters in the original string have now been replaced by noncens.
I know this is due to the fact that the character encoding differ between windows and linux but how can i get around this? Can i somehow specify which character encoding the getBytes()-method should use? I know there is something in java called Charset but i cant figure out how to use it. Anyone got any ideas?
Just look at the Javadoc for the String class. There is a version of getBytes() that takes a String specifying the encoding. There is a Constructor that takes the bytes and a String specifying the encoding. Just make sure you use the same encoding for both. Since utf-8 covers all characters then just use "utf-8" as the encoding for both then it will work anywhere.