Reputation: 3277
Is UTF-8 the default encoding in Java?
If not, how can I know which encoding is used by default?
Upvotes: 72
Views: 145404
Reputation: 3041
You can use this to print out the JVM defaults
import java.nio.charset.Charset;
import java.io.InputStreamReader;
import java.io.FileInputStream;
public class PrintCharSets {
public static void main(String[] args) throws Exception {
System.out.println("file.encoding=" + System.getProperty("file.encoding"));
System.out.println("Charset.defaultCharset=" + Charset.defaultCharset());
System.out.println("InputStreamReader.getEncoding=" + new InputStreamReader(new FileInputStream("./PrintCharSets.java")).getEncoding());
}
}
Compile and Run
javac PrintCharSets.java && java PrintCharSets
Upvotes: 12
Reputation: 436
I am sure that this is JVM implemenation specific, but I was able to "influence" my JVM's default file.encoding by executing:
export LC_ALL=en_US.UTF-8
(running java version 1.7.0_80 on Ubuntu 12.04)
Also, if you type "locale" from your unix console, you should see more info there.
All the credit goes to http://www.philvarner.com/2009/10/24/unicode-in-java-default-charset-part-4/
Upvotes: 15
Reputation: 1093
There are three "default" encodings:
file.encoding:System.getProperty("file.encoding")
java.nio.Charset:Charset.defaultCharset()
And the encoding of the InputStreamReader:InputStreamReader.getEncoding()
You can read more about it on this page.
Upvotes: 32
Reputation: 272297
Note that you can change the default encoding of the JVM using the confusingly-named property file.encoding
.
If your application is particularly sensitive to encodings (perhaps through usage of APIs implying default encodings), then you should explicitly set this on JVM startup to a consistent (known) value.
Upvotes: 36
Reputation: 103797
The default character set of the JVM is that of the system it's running on. There's no specific value for this and you shouldn't generally depend on the default encoding being any particular value.
It can be accessed at runtime via Charset.defaultCharset()
, if that's any use to you, though really you should make a point of always specifying encoding explicitly when you can do so.
Upvotes: 98
Reputation: 403501
It's going to be locale-dependent. Different locale, different default encoding.
Upvotes: 7