本文主要是介绍java 使用CharsetDetector检测文件的编码方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
DEMO1:从服务器上读取文件的内容 判断文件编码
URL url = new URL(getUrl);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();InputStream in = httpConnection.getInputStream();
InputStream tempStream = url.openStream();
CharsetDetector charDect = new CharsetDetector();
String[] unicodeSet = charDect.detectChineseCharset(tempStream);
BufferedReader bufferedReader = null;
if(unicodeSet.length>0)
bufferedReader = new BufferedReader(new InputStreamReader(in,unicodeSet[0]));
else
bufferedReader = new BufferedReader(new InputStreamReader(in,"gb2312"));
StringBuffer temp = new StringBuffer();
String line = bufferedReader.readLine();
while (line != null) {
temp.append(line).append("\r\n");
line = bufferedReader.readLine();
}
bufferedReader.close();
lrc= temp.toString();
DEMO2:判断本地文件编码
public static void main(String[] args){
CharsetDetector charDect = new CharsetDetector();
File f = new File("F:/demo/a.txt");
try {
String[] probableSet = charDect.detectChineseCharset(f.toURL().openStream());
for (String charset : probableSet){
System.out.println(charset);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
封装后的jar包下载地址 http://codeinplatform.googlecode.com/files/CharsetDetector.jar
这篇关于java 使用CharsetDetector检测文件的编码方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!