判断原理:
高版本arcmap(10.2.1及以上)生成的shp文件通常会有一个后缀名.cpg文件生成,如下图所示:

此文件记录着shp文件的编码格式,可用文本编辑器打开该文件查看:

通常低版本arcmap生成的shp文件格式为GBK,高版本为UTF-8,可通过判断是否包含cpg文件或读取cpg文件内容得知编码格式。
实现代码(java):
/**
* 高版本arcgis生成的shp会有一个cpg文件记录编码格式
*
* @param path shp路径
* @return 编码格式
*/
public static Charset getShapeFileCharsetName(String path) {
StringBuilder sb=new StringBuilder(path);
sb.replace(path.length()-4,path.length(),".cpg");
File pFile = new File(sb.toString());
String encode = "GBK";
if (!pFile.exists() || !pFile.isFile()) {
return Charset.forName(encode);
}
try (BufferedReader reader = new BufferedReader(new FileReader(pFile))) {
String tempString;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
if ("UTF-8".equals(tempString.toUpperCase())) {
encode = "UTF-8";
break;
}
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return Charset.forName(encode);
}版权声明:本文为qq_23856901原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。