java中怎么获取、设置文件编码格式?

public class OutputStreamWriterTest {

    public static void main(String[] args) {
        try {
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("e:\\test.txt"));
            osw.write("学海无涯,维勤是岸!!!");
            System.out.println("文件默认编码:" + osw.getEncoding());// 使用getEncoding()方法取得当前系统的默认字符编码
            osw.close();
            /*
             * 如果在调用FileOutputStream的构造方法时没有加入true,那么新加入的字符串就会替换掉原来写入的字符串,
             * 在调用构造方法时指定了字符的编码,新写入的字符,会使用新指定的编码
             */
            osw = new OutputStreamWriter(new FileOutputStream("e:\\test.txt", true), "ISO8859_1");
            osw.write("他山之石,可以攻玉!!!");
            System.out.println("修改文件编码之后getEncoding:" + osw.getEncoding());
            osw.close();
            System.out.println(codeString("e:\\test.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {

        }
    }

    public static String codeString(String fileName) throws Exception {
        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
        int p = (bin.read() << 8) + bin.read();
        String code = null;
        // 其中的 0xefbb、0xfffe、0xfeff、0x5c75这些都是这个文件的前面两个字节的16进制数
        switch (p) {
        case 0xefbb:
            code = "UTF-8";
            break;
        case 0xfffe:
            code = "Unicode";
            break;
        case 0xfeff:
            code = "UTF-16BE";
            break;
        case 0x5c75:
            code = "ANSI|ASCII";
            break;
        default:
            code = "GBK";
        }
        return code;
    }
}

运行程序后输出:

文件默认编码:UTF8
修改文件编码之后getEncoding:ISO8859_1
GBK

问题1:java 中是怎么设置文件的编码格式?有没有方法能指定新增文件编码格式?修改已存在的文件编码格式?
问题2:上面程序中创建的文件,到底是那种编码格式的?

请大神赐教,感激不尽……

阅读 15.7k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进