Příklad

public static void main( String[] args ) {
// Předpokládejme výchozí znakovou sadu ISO-8859-1 a soubor s obsahem UTF-16BE
FileReader fileReader = null;
try {
fileReader = new java.io.FileReader( "file1" ); //$NON-NLS-1$
System.out.println( fileReader.read() );
} catch ( IOException e ) {
e.printStackTrace();
}
}



Řešení
Abyste mohli zadat hodnoty kódování znaků sami, zkonstruujte InputStreamReader s CharsetDecoder na FileInputStream.

public static void main( String[] args ) {
// Předpokládejme výchozí znakovou sadu ISO-8859-1 a soubor s obsahem UTF-16BE
InputStreamReader reader = null;
try {
reader = new InputStreamReader( new FileInputStream("file1"), "UTF-16BE");
System.out.println( reader.read() );
} catch ( IOException e ) {
e.printStackTrace();
}

}