java判断是否是乱码
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MessyCodeCheck {
public static void main(String[] artb){
//System.out.println(new Integer(""));
System.out.println(isMessyCode("ڵÄÍøվÒòΪÂÇÓҲàÂô¹"));
System.out.println(isMessyCode("*��JTP.jar�ļ����JTPsdfasdfadgasgasdgas�ļ���ȡ��ͼƬ��Դ"));
System.out.println(isMessyCode("���������µ�ó�ױ��ݡ������ƽ����۷�����dz��������ɹ����dz��� ����ա��涨�ڵȸ��������ˮ��[��ϸ]1֤�������ش����±䶯 ?�����⽫"));
}
// GENERAL_PUNCTUATION 判断中文的“号 //
// CJK_SYMBOLS_AND_PUNCTUATION 判断中文 的。号 /
// HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的,号
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
public static boolean isMessyCode(String strName) {
Pattern p = Pattern.compile("*");
Matcher m = p.matcher(strName);
String after = m.replaceAll("");
System.out.println("after=======" + after);
System.out.println("=======================");
String temp = after.replaceAll("}", "");
System.out.println("temp=======" + temp);
char[] ch = temp.trim().toCharArray();
float chLength = ch.length;
System.out.println("chLength=" + chLength);
float count = 0;
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (!Character.isLetterOrDigit(c)) { // 确定指定字符是否为字母或数字
if (!isChinese(c)) {
count = count + 1;
System.out.print(c);
}
}
}
System.out.println("count=" + count);
float result = count / chLength;
if (result > 0.4) {
return true;
} else {
return false;
}
}
}