ITeye博客

java检查文本文件编码格式

rabby · 2011-05-24 22:02 · 编程语言 · Markdown

public static String getEncoding(File file) {
		String encoding = "GBK";
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(file);
			byte[] b = new byte[3];
			fileInputStream.read(b);
			if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
				encoding = "UTF-8";
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != fileInputStream) {
				try {
					fileInputStream.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return encoding;
	}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。