Gets default file encoding - Java java.io

Java examples for java.io:File UTF

Description

Gets default file encoding

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    /**//from   w w  w  .ja v a  2 s . com
     * Gets default encoding
     * 
     * @return Default Encoding
     */
    public static String getDefaultEncoding() {
        String encoding = System.getProperty("file.encoding");
        if (encoding == null) {
            try {
                File file = File.createTempFile("temp", null);
                FileWriter writer = new FileWriter(file);
                encoding = writer.getEncoding();
                writer.close();
                file.deleteOnExit();
            } catch (IOException e) {
                encoding = "US-ASCII";
            }
        }
        return encoding;
    }
}

Related Tutorials