Changing 'System.out' encoding

S

Stanimir Stamenkov

I've searched the archives but haven't found any useful information.

I'm trying to change the encoding of the default 'System.out' output
stream so I could see some international characters in the text console.

I'm trying this on Win2000:

public class EncodingTest {

public static void main(String[] args) {
String cyrStr = "\u0430\u0431\u0432\u0433\u0434\u0435";
String latStr = "\u00E0\u00E1\u00E2\u00E9\u00EA\u00EB";

System.out.println(System.getProperty("file.encoding"));
System.out.println(cyrStr);
System.out.println(latStr);
JOptionPane.showMessageDialog(null, cyrStr + "\n" + latStr);
System.exit(0);
}

}

So my default encoding seems to be "Cp1251" (because of my regional
settings) and on the command prompt I type:

C:\>chcp 1251
C:\>java EncodingTest

and I get:

Cp1251
абвгде
??????

then I want to see the latin supplement characters and I type:

C:\>chcp 1252
C:\>java -Dfile.encoding=Cp1252 EncodingTest

and I get:

Cp1252
àáâãäå
??????

So is it possible to change the encoding of the default 'System.out'?
 
Y

Yu SONG

Stanimir said:
I've searched the archives but haven't found any useful information.

I'm trying to change the encoding of the default 'System.out' output
stream so I could see some international characters in the text console.

I'm trying this on Win2000:

public class EncodingTest {

public static void main(String[] args) {
String cyrStr = "\u0430\u0431\u0432\u0433\u0434\u0435";
String latStr = "\u00E0\u00E1\u00E2\u00E9\u00EA\u00EB";

System.out.println(System.getProperty("file.encoding"));
System.out.println(cyrStr);
System.out.println(latStr);
JOptionPane.showMessageDialog(null, cyrStr + "\n" + latStr);
System.exit(0);
}

}

So my default encoding seems to be "Cp1251" (because of my regional
settings) and on the command prompt I type:

C:\>chcp 1251
C:\>java EncodingTest

and I get:

Cp1251
абвгде
??????

then I want to see the latin supplement characters and I type:

C:\>chcp 1252
C:\>java -Dfile.encoding=Cp1252 EncodingTest

and I get:

Cp1252
àáâãäå
??????

So is it possible to change the encoding of the default 'System.out'?

Redirect "System.out" to a *PrintStream*, so that you can change the
encoding.

--
Song

/* E-mail.c */
#define User "Yu.Song"
#define At '@'
#define Warwick "warwick.ac.uk"
int main() {
printf("Yu Song's E-mail: %s%c%s", User, At, Warwick);
return 0;}

Further Info. : http://www.dcs.warwick.ac.uk/~esubbn/
_______________________________________________________
 
J

John Davison

Stanimir said:
I'm trying to change the encoding of the default 'System.out' output
stream so I could see some international characters in the text console.

-- snippy --
So is it possible to change the encoding of the default 'System.out'?

If you look at the source code, you'll see that the System.out is final,
so you can't change that without recompiling the library (and I
wouldn't suggest doing that.) Here's what I came up with.

import java.io.*;

public class test {
public static void main(String[] args) {

PrintStream ps = null;

try {
ps = new PrintStream(System.out, true, "ISO-8859-1");
} catch (UnsupportedEncodingException error) {
System.err.println(error);
System.exit(0);
}

ps.println("\u00E0\u00E1\u00E2\u00E9\u00EA\u00EB");
}
}

where "ISO-8859-1" is replaced by whatever character encoding you want.

John Davison
Compass Engineering Group
 
J

John C. Bollinger

Stanimir said:
I've searched the archives but haven't found any useful information.

I'm trying to change the encoding of the default 'System.out' output
stream so I could see some international characters in the text console.
[...]

So is it possible to change the encoding of the default 'System.out'?

Lookup the type of System.out: it is a PrintStream, which is a subclass
of OutputStream. OutputStreams are _byte_ streams. There is no
relevant concept of a character encoding. You can, however, do this:

PrintWriter out =
new PrintWriter(
new OutputStreamWriter(System.out, "Cp1251"));

[You will need to be prepared to handle an UnsupportedEncodingException.]

Thereafter write to your PrintWriter out, instead of directly to
System.out, and you should be in good shape. [Note that I make no
representation as to the console's ability to correctly display the
resulting byte stream; I assume you have that under control.]


John Bollinger
(e-mail address removed)
 
S

Stanimir Stamenkov

/John C. Bollinger/:
PrintWriter out =
new PrintWriter(
new OutputStreamWriter(System.out, "Cp1251"));

Huh... Thank you you, John (and thank you to John Davidson, too). :)

You know guys, this was the first thing I've tried but in this form:

Writer out = new OutputStreamWriter(System.out, "ISO-8859-1");
out.write("\u00E0\u00E1\u00E2\u00E9\u00EA\u00EB");

and it just don't work (no output at all), that's why I've decided
to post. But then using:

PrintStream out = new PrintStream(System.out, true, "ISO-8859-1");
out.println("\u00E0\u00E1\u00E2\u00E9\u00EA\u00EB");

does it just fine - thank you, again.
 
Joined
Jan 2, 2013
Messages
2
Reaction score
0
My code which is simply casting the ascii character to get decimal value and it is working properly. For example:

public static void main(String[] args) {
char character = 'A';
int intChar = (int) character;
System.out.println("Decimal Value is " + intChar);
}

Which returns me correct output as below:
Decimal Value is 65

Above code is working fine when characters range is from 0 to 127 (US-ASCII). But when I am trying to get the decimal value for extended ascii characters it
returns me wrong decimal number. For example

public static void main(String[] args) {
char character = 'Š';
int intChar = (int) character;
System.out.println("Decimal Value is " + intChar);
}
Which returns me incorrect output as below:
Decimal Value is 352

Instead it should return 138 as decimal value. 352 is the Unicode value for character 'Š' but I am expecting 138 which is correct value. Please help me to achieve this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top