fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.nio.charset.StandardCharsets ;
  8. import java.nio.charset.Charset ;
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15. String blurb = "© 2021 ABC Inc. All rights reserved." ;
  16.  
  17. // The character set and encoding currently in use by `System.out` is not known, some default.
  18. System.out.println( "----------| default |--------------------------" );
  19. System.out.println( "blurb: " + blurb ) ;
  20.  
  21. // Let's set the character set and encoding to UTF-8 by wrapping `System.out` in a `PrintStream`.
  22. System.out.println( "----------| UTF-8 |--------------------------" );
  23. try
  24. {
  25. PrintStream printStream = new PrintStream( System.out , true , StandardCharsets.UTF_8.name() );
  26. printStream.println( "blurb: " + blurb );
  27. }
  28. {
  29. e.printStackTrace();
  30. }
  31.  
  32. // In contrast, try Windows-1252 character set.
  33. System.out.println( "----------| windows-1252 |--------------------------" );
  34.  
  35. // Verify windows-1252 charset is available on the current JVM.
  36. String windows1252CharSetName = "windows-1252";
  37. boolean isWindows1252CharsetAvailable = Charset.availableCharsets().keySet().contains( windows1252CharSetName );
  38. if ( isWindows1252CharsetAvailable )
  39. {
  40. System.out.println( "isWindows1252CharsetAvailable = " + isWindows1252CharsetAvailable );
  41. } else
  42. {
  43. System.out.println( "FAIL - No charset available for name: " + windows1252CharSetName );
  44. }
  45.  
  46. // Print the blurb.
  47. try
  48. {
  49. PrintStream printStream = new PrintStream( System.out , true , windows1252CharSetName );
  50. printStream.println( "blurb: " + blurb );
  51. }
  52. {
  53. e.printStackTrace();
  54. }
  55.  
  56. }
  57. }
Success #stdin #stdout 0.33s 48364KB
stdin
Standard input is empty
stdout
----------|  default  |--------------------------
blurb: © 2021 ABC Inc. All rights reserved.
----------|  UTF-8  |--------------------------
blurb: © 2021 ABC Inc. All rights reserved.
----------|  windows-1252  |--------------------------
isWindows1252CharsetAvailable = true
blurb: � 2021 ABC Inc. All rights reserved.