Java FileWriter getEncoding() Method
Example
Print the name of the character encoding, before and after the writer is closed:
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");
System.out.println(myWriter.getEncoding());
myWriter.close();
System.out.println(myWriter.getEncoding());
}
}
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 characters into the bytes that are saved in the file) that the writer uses.
A FileWriter uses the default encoding of the computer, so the first line can be different on your computer, for example Cp1252. If the writer 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 writer is closed. |
|---|
Related Pages
❮ FileWriter Methods