收集JAVA常用函数代码
1
/** */
/**
2
* 判断是不是合法手机
3
* handset 手机号码
4
*/
5![]()
public
static
boolean
isHandset(String handset)
{
6![]()
try
{
7![]()
if(!handset.substring(0,1).equals("1"))
{
8
return false;
9
}
10![]()
if (handset==null || handset.length()!=11)
{
11
return false;
12
}
13
String check = "^[0123456789]+$";
14
Pattern regex = Pattern.compile(check);
15
Matcher matcher = regex.matcher(handset);
16
boolean isMatched = matcher.matches();
17![]()
if(isMatched)
{
18
return true;
19![]()
} else
{
20
return false;
21
}
22![]()
} catch (RuntimeException e)
{
23
return false;
24
}
25
}
26
}
27![]()
28
2
3
4
5

6

7

8
9
10

11
12
13
14
15
16
17

18
19

20
21
22

23
24
25
26
27
28
1
/** */
/**
2
* 将指定byte数组以16进制的形式打印到控制台
3
* @param hint
4
* String
5
* @param b
6
* byte[]
7
* @return void
8
*/
9
public
static
void
printHexString(String hint,
byte
[] b)
10![]()
{
11
System.out.print(hint);
12
for (int i = 0; i < b.length; i++)
13![]()
{
14
String hex = Integer.toHexString(b[i] & 0xFF);
15
if (hex.length() == 1)
16![]()
{
17
hex = '0' + hex;
18
}
19
System.out.print(hex.toUpperCase() + " ");
20
}
21
System.out.println("");
22
}
23
2
3
4
5
6
7
8
9
10

11
12
13

14
15
16

17
18
19
20
21
22
23
1
/** */
/**
2
* 全角字符转半角字符
3
*
4
* @param QJStr
5
* @return String
6
*/
7
public
static
final
String QJToBJChange(String QJStr)
8![]()
{
9
char[] chr = QJStr.toCharArray();
10
String str = "";
11
for (int i = 0; i < chr.length; i++)
12![]()
{
13
chr[i] = (char) ((int) chr[i] - 65248);
14
str += chr[i];
15
}
16
return str;
17
}
2
3
4
5
6
7
8

9
10
11
12

13
14
15
16
17
1
/** */
/**
2
* 去掉字符串中重复的子字符串
3
*
4
* @param str
5
* @return String
6
*/
7
private
static
String removeSameString(String str)
8![]()
{
9
Set<String> mLinkedSet = new LinkedHashSet<String>();
10
String[] strArray = str.split(" ");
11
StringBuffer sb = new StringBuffer();
12![]()
13
for (int i = 0; i < strArray.length; i++)
14![]()
{
15
if (!mLinkedSet.contains(strArray[i]))
16![]()
{
17
mLinkedSet.add(strArray[i]);
18
sb.append(strArray[i] + " ");
19
}
20
}
21
System.out.println(mLinkedSet);
22
return sb.toString().substring(0, sb.toString().length() - 1);
23
}
2
3
4
5
6
7
8

9
10
11
12
13
14

15
16

17
18
19
20
21
22
23
1
/** */
/**
2
* 设置JSpinner的编辑属性
3
* @param spinner 目标JSpinner
4
* @param isAllowInvalid 是否允许输入非法值
5
* @param isEditable 是否允许编辑
6
*/
7
public
static
void
setAllowsInvalid(JSpinner spinner,
boolean
isAllowInvalid,
boolean
isEditable)
8![]()
{
9
JSpinner.NumberEditor editor = new JSpinner.NumberEditor(spinner, "#");
10
spinner.setEditor(editor);
11
JFormattedTextField tf = ((JSpinner.NumberEditor)spinner.getEditor()).getTextField();
12
tf.setEditable(isEditable);
13
DefaultFormatterFactory factory = (DefaultFormatterFactory)tf.getFormatterFactory();
14
NumberFormatter formatter = (NumberFormatter)factory.getDefaultFormatter();
15
formatter.setAllowsInvalid(isAllowInvalid);
16
}
17
2
3
4
5
6
7
8

9
10
11
12
13
14
15
16
17
1
/** */
/**
2
* 根据指定方法的参数去构造一个新的对象的拷贝并将他返回
3
* @param obj 原始对象
4
* @return 新对象
5
* @throws NoSuchMethodException
6
* @throws InvocationTargetException
7
* @throws IllegalAccessException
8
* @throws InstantiationException
9
* @throws SecurityException
10
* @throws IllegalArgumentException
11
*/
12
@SuppressWarnings(
"
unchecked
"
)
13
public
static
Object copy(Object obj)
throws
IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException,
14
InvocationTargetException, NoSuchMethodException
15![]()
{
16
//获得对象的类型
17
Class classType = obj.getClass();
18![]()
19
//通过默认构造方法去创建一个新的对象,getConstructor的视其参数决定调用哪个构造方法
20![]()
Object objectCopy = classType.getConstructor(new Class[]
{}).newInstance(new Object[]
{});
21![]()
22
//获得对象的所有属性
23
Field[] fields = classType.getDeclaredFields();
24![]()
25
for(int i = 0; i < fields.length; i++)
26![]()
{
27
//获取数组中对应的属性
28
Field field = fields[i];
29![]()
30
String fieldName = field.getName();
31
String stringLetter = fieldName.substring(0, 1).toUpperCase();
32![]()
33
//获得相应属性的getXXX和setXXX方法名称
34
String getName = "get" + stringLetter + fieldName.substring(1);
35
String setName = "set" + stringLetter + fieldName.substring(1);
36![]()
37
//获取相应的方法
38![]()
Method getMethod = classType.getMethod(getName, new Class[]
{});
39![]()
Method setMethod = classType.getMethod(setName, new Class[]
{field.getType()});
40![]()
41
//调用源对象的getXXX()方法
42![]()
Object value = getMethod.invoke(obj, new Object[]
{});
43![]()
44
//调用拷贝对象的setXXX()方法
45![]()
setMethod.invoke(objectCopy, new Object[]
{value});
46
}
47![]()
48
return objectCopy;
49
}
50![]()
51
2
3
4
5
6
7
8
9
10
11
12
13
14
15

16
17
18
19
20


21
22
23
24
25
26

27
28
29
30
31
32
33
34
35
36
37
38

39

40
41
42

43
44
45

46
47
48
49
50
51
1
//
过滤特殊字符
2![]()
public
static
String encoding(String src)
{
3
if (src==null)
4
return "";
5
StringBuilder result=new StringBuilder();
6![]()
if (src!=null)
{
7
src=src.trim();
8![]()
for (int pos=0;pos<src.length();pos++)
{
9![]()
switch(src.charAt(pos))
{
10
case '\"':result.append(""");break;
11
case '<':result.append("<");break;
12
case '>':result.append(">");break;
13
case '\'':result.append("'");break;
14
case '&':result.append("&");break;
15
case '%':result.append("&pc;");break;
16
case '_':result.append("&ul;");break;
17
case '#':result.append("&shap;");break;
18
case '?':result.append("&ques;");break;
19
default:result.append(src.charAt(pos));break;
20
}
21
}
22
}
23
return result.toString();
24
}
25
26
//
反过滤特殊字符
27![]()
public
static
String decoding(String src)
{
28
if (src==null)
29
return "";
30
String result=src;
31
result=result.replace(""", "\"").replace("'", "\'");
32
result=result.replace("<", "<").replace(">", ">");
33
result=result.replace("&", "&");
34
result=result.replace("&pc;", "%").replace("&ul", "_");
35
result=result.replace("&shap;", "#").replace("&ques", "?");
36
return result;
37
}
38
2

3
4
5
6

7
8

9

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

28
29
30
31
32
33
34
35
36
37
38
1
/** */
/**
2
* 写入日志
3
* filePath 日志文件的路径
4
* code 要写入日志文件的内容
5
*/
6![]()
public
static
boolean
print(String filePath,String code)
{
7![]()
try
{
8
File tofile=new File(filePath);
9
FileWriter fw=new FileWriter(tofile,true);
10
BufferedWriter bw=new BufferedWriter(fw);
11
PrintWriter pw=new PrintWriter(bw);
12
13
System.out.println(getDate()+":"+code);
14
pw.println(getDate()+":"+code);
15
pw.close();
16
bw.close();
17
fw.close();
18
return true;
19![]()
} catch (IOException e)
{
20
return false;
21
}
22
}
23
2
3
4
5
6

7

8
9
10
11
12
13
14
15
16
17
18
19

20
21
22
23
1
//
字符串匹配的算法.
2![]()
public
String getMaxMatch(String a,String b)
{
3
StringBuffer tmp = new StringBuffer();
4
String maxString = "";
5
int max = 0;
6
int len = 0;
7
char[] aArray = a.toCharArray();
8
char[] bArray = b.toCharArray();
9
int posA = 0;
10
int posB = 0;
11![]()
while(posA<aArray.length-max)
{
12
posB = 0;
13![]()
while(posB<(bArray.length-max))
{
14![]()
if(aArray[posA]==bArray[posB])
{
15
len = 1;
16
tmp = new StringBuffer();
17
tmp.append(aArray[posA]);
18![]()
while((posA+len<aArray.length)&&(posB+len<bArray.length)&&(aArray[posA+len]==bArray[posB+len]))
{
19
tmp.append(aArray[posA+len]);
20
len++;
21
}
22![]()
if(len>max)
{
23
max = len;
24
maxString = tmp.toString();
25
}
26
}
27
posB++;
28
}
29
posA++;
30
}
31
return maxString;
32
}
33![]()
34
2

3
4
5
6
7
8
9
10
11

12
13

14

15
16
17
18

19
20
21
22

23
24
25
26
27
28
29
30
31
32
33
34