---
title: "如何判断文本是否是UTF8编码"
author: "xinyiwust"
date: "2012-08-27T16:44:10Z"
tags: ["java charset 编码"]
categories: ["编程语言"]
slug: "utf8"
draft: false
lastmod: "2012-08-27T16:44:10Z"
id: 1664157
url: "https://xinyiwust.iteye.com/blog/1664157"
---

UTF8格式的文本开头的三个字节是-17、-69和-65，主要读取文件或流的前三个字节，然后比对即可：

```java

	/**
	 * @param byte0 the first byte of file(or stream)
	 * @param byte1 the second byte of file(or stream)
	 * @param byte2 the third byte of file(or stream)
	 * @return return true if file(or stream) is coded by UTF8,else return false
	 */
	public static boolean isUTF8(byte byte0,byte byte1,byte byte2){
		if(-17 == byte0 && -69 == byte1 && -65 == byte2)
			return true;
		return false;
	}

```