-
大小: 176KB文件类型: .java金币: 1下载: 0 次发布日期: 2021-05-28
- 语言: Java
- 标签: EncodingDete
资源简介
检测文件编码类型的工具类,用Java语言描述,如有需要请下载。不知道积分怎么越来越高,已经违背了本人分享的初衷,没有积分的可到博客评论留下邮箱
代码片段和文件信息
package com.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
public class EncodingDetect {
public static String getJavaEncode(String filePath) {
BytesEncodingDetect s = new BytesEncodingDetect();
String fileCode = BytesEncodingDetect.javaname[s.detectEncoding(new File(filePath))];
return fileCode;
}
public static void writeFile(String path String content String charSet) {
try {
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path) charSet);
out.write(content);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取文件
*/
public static String readFile(String file boolean... isTrims) {
StringBuffer buffer = new StringBuffer();
String code = getJavaEncode(file);
BufferedReader fr;
boolean isTrim = isTrims.length > 0 ? isTrims[0] : false;
String line_separator = System.getProperty(“line.separator“);
try {
String myCode = code != null && !““.equals(code) ? code : “UTF-8“;
InputStreamReader read = new InputStreamReader(new FileInputStream(file) myCode);
fr = new BufferedReader(read);
String line = null;
int flag = 1;
while ((line = fr.readLine()) != null) {
if (!isTrim) {
if (flag != 1) {
buffer.append(line_separator);
}
}
flag++;
buffer.append(isTrim ? line.isEmpty() : line);
}
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}
}
class BytesEncodingDetect extends Encoding {
int GBFreq[][];
int GBKFreq[][];
int Big5Freq[][];
int Big5PFreq[][];
int EUC_TWFreq[][];
int KRFreq[][];
int JPFreq[][];
public boolean debug;
public BytesEncodingDetect() {
super();
debug = false;
GBFreq = new int[94][94];
GBKFreq = new int[126][191];
Big5Freq = new int[94][158];
Big5PFreq = new int[126][191];
EUC_TWFreq = new int[94][94];
KRFreq = new int[94][94];
JPFreq = new int[94][94];
initialize_frequencies();
}
public static void main(String argc[]) {
BytesEncodingDetect sinodetector;
int result = OTHER;
int i;
sinodetector = new BytesEncodingDetect();
for (i = 0; i < argc.length; i++) {
if (argc[i].startsWith(“http://“) == true) {
try {
- 上一篇:javaweb一键部署批处理文件
- 下一篇:模拟CSMA/CD的过程
评论
共有 条评论