Java FileReader getEncoding() Method
Example
Print the name of the character encoding:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Hello World");
myWriter.close();
FileReader myReader = new FileReader("filename.txt");
System.out.println(myReader.getEncoding());
myReader.close();
}
}
Note: The output depends on the computer and its settings.
Definition and Usage
The getEncoding() method returns the name of the character encoding (the rule that turns the bytes in the file into characters) that the reader uses.
A FileReader uses the default encoding of the computer, so the result can be different on your computer, for example UTF8 or Cp1252. If the reader is closed, getEncoding() returns null.
Syntax
public String getEncoding()
Parameter Values
None.
Technical Details
| Returns: | The name of the encoding as a String, or null if the reader is closed. |
|---|
Related Pages
❮ FileReader Methods