Java examples for Internationalization:Charset
Get String encode for text file
//package com.java2s; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; public class Main { public static void main(String[] argv) throws Exception { String fileName = "java2s.com"; System.out.println(codeString(fileName)); }/*from ww w . ja v a2 s. co m*/ public static String codeString(String fileName) { String code = "utf-8"; try { FileInputStream fileInputStream = new FileInputStream(fileName); BufferedInputStream bufferedInputStream = new BufferedInputStream( new FileInputStream(fileName)); int p = (bufferedInputStream.read() << 8) + bufferedInputStream.read(); switch (p) { case 0xefbb: code = "utf-8"; break; case 0xfffe: code = "Unicode"; break; case 0xfeff: code = "UTF-16BE"; break; default: code = "utf-8"; break; } if (bufferedInputStream != null) { bufferedInputStream.close(); } if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } return code; } }