java碎笔

时间:2023-03-09 14:46:46
java碎笔

选择表达式

overviewPart1.setMonth_incom(rs.getString("month_incom").equals("")?"0":rs.getString("month_incom"));

Int和String互换

1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);

1). int i = Integer.parseInt([String]); 或

i = Integer.parseInt([String],[int radix]);

2). int i = Integer.valueOf(my_str).intValue();

List与数组互换

String[] arr = new String[] {"str1", "str2"};
List<String> list = Arrays.asList(arr);
final int size = list.size();
String[] arr = (String[])list.toArray(new String[size]);

Split 多个分隔符:

String[] value = line.split(" ");
1、分隔符为“.”(无输出),“|”(不能得到正确结果)转义字符时,“*”,“+”时出错抛出异常,都必须在前面加必须得加"\\",如split(\\|);
2、如果用"\"作为分隔,就得写成这样:String.split("\\\\"),因为在Java中是用"\\"来表示"\"的,字符串得写成这样:String Str="a\\b\\c";
?转义字符,必须得加"\\";
3、如果在一个字符串中有多个分隔符,可以用"|"作为连字符,比如:String str="Java string-split#test",可以用Str.split(" |-|#")把每个字符串分开;

  注意:特别是同时存在一个空格和两个空格时要注意,先replaceAll,再split

java正则表达式

参考分类网络学习中java正则表达式:

	public static void main(String[] srgs){
String strline2= "123-12-12342323";
String regex2 ="[0-9]{3}\\-[0-9]{2}\\-[0-9]{4}";
String strline1= "11:08:56.149361 IP ";
String regex1 ="([0-2][0-4]):([0-5][0-9]):([0-5][0-9]).[0-9]{6}";
String strline3= "0x0000: 4500 003c c437 4000 4006 63cb c0a8 0078 E..<.7@.@.c....x";
String regex3 ="0x[0-9]{4}: ([A-Za-z0-9]{4} )+";
Pattern pattern1=Pattern.compile(regex3);
Matcher match1=pattern1.matcher(strline3);
while(match1.find()){
System.out.println(match1.group());
String hexline = match1.group();
String regexHex2 ="([A-Za-z0-9]{4} )+";
Pattern patternHex2 = Pattern.compile(regexHex2);
Matcher matchHex2 = patternHex2.matcher(hexline);
while (matchHex2.find()) {
System.out.println(matchHex2.group());
}
}
}

  

写文件

读文件参考博文—java读取文件乱码

// 写文件 append方式
public static boolean writeFile(String file, String fileContent) { try {
BufferedWriter buffwrite = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(file, true),
"utf-8"));
buffwrite.write(fileContent);
buffwrite.flush();
buffwrite.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}