Manitoba
Manitoba

Reputation: 8702

Java - Encoding String

I'm trying to read the flow from the system console but I have some problems with the encoding. As I'm french, my keyboard has some special chars such as 'é', 'è', 'à', etc...

When I put the content of the console into a text area, I got something like that:

Le volume dans le lecteur C s'appelle DisqueC
Le num?ro de s?rie du volume est CE7........
3 fichier(s)           20?229 octets
5 R?p(s)  450?096?623?616 octets libres

It automaticaly replaces special chars by '?' and there is also a problem with the size.

I read the content of the console like that:

BufferedReader stdInput = new BufferedReader(new InputStreamReader(child.getInputStream(), "UTF-8"));
BufferedReader stdError = new BufferedReader(new InputStreamReader(child.getErrorStream(), "UTF-8"));

String line;
byte[] lineBytes;
while ((line = stdInput.readLine()) != null)
{
    lineBytes = line.getBytes("UTF-8");
    buffer.append("\t\t" + new String(lineBytes, "UTF-8") + "\n");
}
stdInput.close();
while ((line = stdError.readLine()) != null)
{
lineBytes = line.getBytes("UTF-8");
buffer.append("\t\t" + new String(lineBytes, "UTF-8") + "\n");
}
stdError.close();

Then the buffer is sent through a socket and I display it like that into a jTextArea:

textArea.append(new String(console_output.getBytes(), "UTF-8"););

So, can anyone tell me why I have this problem with encoding?

Thanks.

Upvotes: 0

Views: 560

Answers (2)

user3355867
user3355867

Reputation: 1

In some cases this function will be well:

System.out.println(String.format(Locale.UK, <your String value>, args));

Upvotes: 0

Michael-O
Michael-O

Reputation: 18415

If you are doing it on Windows, you should forget that. The default Command Line Encoding ist CP850 which is crap. You can change that for the session but you won't be able to call any batch files.

Try chcp 65001

This will switch to UTF-8. Check this StackOveflow search.

Upvotes: 1

Related Questions