A.Raw
A.Raw

Reputation: 27

Java Encoding issue

When defining the below string on Java JDK 9 String s = "एक गाव में एक किसान" It throws the following error: Unmappable character (0xE0) for encoding US-ASCII I understand that it is UTF-8 encoded but as JDK 9 has default charset set as US-ASCII but I can't find how to change the default charset to UTF-8 from the code itself?

Upvotes: 0

Views: 191

Answers (1)

Andreas
Andreas

Reputation: 159086

I want to change the default charset from within the code

Impossible1.

The source code is just text, it doesn't define the encoding used to store that text.

You can however decide that your source code is US-ASCII, so the encoding doesn't matter (excl. UTF-16, UTF-32, etc), by specifying all non-ASCII characters as Unicode escapes:

String s = "\u090f\u0915 \u0917\u093e\u0935 \u092e\u0947\u0902 \u090f\u0915 \u0915\u093f\u0938\u093e\u0928"

Of course, it makes it difficult to know what the string says.

1) Since Java doesn't support BOM's.

Upvotes: 2

Related Questions