Reading Writing Text  «Prev  Next»


Lesson 9The getEncoding() method
ObjectiveFamiliarize yourself with the use of the getEncoding method.

Java getEncoding Method

There is a getEncoding() method, which simply tells you what encoding this reader is using.
public String getEncoding()
This method in conjunction with the InputStreamReader constructor can be used to determine the default encoding, like this:

InputStreamReader isr = new InputStreamReader(System.in);
System.out.println("The default encoding is " + isr.getEncoding());

Here I have taken advantage of the fact that the default encoding is used when no encoding is passed to the InputStreamReader constructor.

Unicode is a fairly large character set; most other character sets don't have all the characters in Unicode. Attempts to write characters that don't exist in a given set instead produce a substitution character, generally a question mark.
The getEncoding() method returns a string containing the name of the encoding used by this writer:
public String getEncoding()

363 The flush() and close() methods flush and close the underlying output stream.
public void flush() throws IOException
public void close() throws IOException