Last active
January 6, 2016 16:37
-
-
Save jyeary/9623997970ae4ebc0df6 to your computer and use it in GitHub Desktop.
Check the encoding (default) of the current Java implementation on the system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.nio.charset.Charset; | |
public class CheckEncoding { | |
public static void main(String[] args) { | |
System.out.println("Default Charset = " + getDefaultCharset()); | |
System.out.println("System Property (file.encoding) = " + getFileEncoding()); | |
System.out.println("Default Charset in Writer = " + getWriterEncoding()); | |
System.out.println("Default Charset in Reader = " + getReaderEncoding()); | |
} | |
public static String getWriterEncoding() { | |
OutputStreamWriter writer = new OutputStreamWriter(new ByteArrayOutputStream()); | |
String encoding = writer.getEncoding(); | |
return encoding; | |
} | |
public static String getReaderEncoding() { | |
byte[] buffer = new byte[8]; | |
InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(buffer)); | |
String encoding = reader.getEncoding(); | |
return encoding; | |
} | |
public static String getDefaultCharset() { | |
return Charset.defaultCharset().name(); | |
} | |
public static String getFileEncoding() { | |
return System.getProperty("file.encoding"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment