一、前言
定义DataValidator数据有效性验证工具类,基于正则表达式实现数值数字整数验证(isIntege1/isNum)、精度小数验证(isDecmal/)、颜色验证(isColor)、URL地址验证(isUrl)、中文汉字验证(isChinese)、ASCii码验证(isAscii)、手机号验证(isMobile)、IP地址验证(isIp)、空值验证(isNotempty)、图片后缀验证(isPicture)、压缩包验证(isRar)、日期格式验证(isDate)、QQ号码验证(isQq)、电话验证(isTel)、用户名验证(isUsername)、字母验证(isLetter)、金钱验证(isPrice)。
二、代码示例
package validator;@b@@b@import java.util.Hashtable;@b@import java.util.regex.Matcher;@b@import java.util.regex.Pattern;@b@@b@import java.io.IOException;@b@import java.io.PrintWriter;@b@import java.io.StringWriter;@b@import java.text.ParseException;@b@import java.text.SimpleDateFormat;@b@import java.util.Calendar;@b@import java.util.Date;@b@import java.util.GregorianCalendar;@b@import java.util.Hashtable;@b@import java.util.regex.Matcher;@b@import java.util.regex.Pattern;@b@@b@public class DataValidator {@b@@b@ public static boolean isIntege(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^-?[1-9]\\d*$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@@b@ public static boolean isIntege1(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^[1-9]\\d*$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@@b@ public static boolean isIntege2(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^-[1-9]\\d*$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@@b@ public static boolean isNum(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^([+-]?)\\d*\\.?\\d+$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@@b@ public static boolean isNum1(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^[1-9]\\d*|0$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@@b@ public static boolean isNum2(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^-[1-9]\\d*|0$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@@b@ public static boolean isDecmal(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^([+-]?)\\d*\\.\\d+$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@@b@ public static boolean isDecmal1(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@@b@ public static boolean isDecmal2(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@@b@ public static boolean isDecmal3(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@@b@ public static boolean isDecmal4(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@@b@ public static boolean isDecmal5(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //邮箱校验@b@ public static boolean isEmail(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern@b@ .compile("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //颜色校验@b@ public static boolean isColor(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^[a-fA-F0-9]{6}$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //url地址校验@b@ public static boolean isUrl(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern@b@ .compile("^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //中文校验@b@ public static boolean isChinese(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //ascii码校验@b@ public static boolean isAscii(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^[\\x00-\\xFF]+$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //zip编码校验@b@ public static boolean isZipcode(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^\\d{6}$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //手机号验证@b@ public static boolean isMobile(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^(13|15)[0-9]{9}$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //ip地址校验@b@ public static boolean isIp(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern@b@ .compile("^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@@b@ /**@b@ * 是否是ipv4地址@b@ *@b@ * @param str 字符串@b@ * @return@b@ */@b@ public final static boolean isIPv4Address(String str) {@b@ if (str != null) {@b@ int dot1 = str.indexOf('.');@b@ if (dot1 <= 0) {@b@ return false;@b@ }@b@ int temp;@b@ try {@b@ temp = Integer.parseInt(str.substring(0, dot1++));@b@ if (temp < 0 || temp > 255) {@b@ return false;@b@ }@b@ } catch (Exception ex) {@b@ return false;@b@ }@b@@b@ int dot2 = str.indexOf('.', dot1);@b@ if (dot2 <= 0) {@b@ return false;@b@ }@b@ try {@b@ temp = Integer.parseInt(str.substring(dot1, dot2++));@b@ if (temp < 0 || temp > 255) {@b@ return false;@b@ }@b@ } catch (Exception ex) {@b@ return false;@b@ }@b@@b@ int dot3 = str.indexOf('.', dot2);@b@ if (dot3 <= 0) {@b@ return false;@b@ }@b@ try {@b@ temp = Integer.parseInt(str.substring(dot2, dot3++));@b@ if (temp < 0 || temp > 255) {@b@ return false;@b@ }@b@ } catch (Exception ex) {@b@ return false;@b@ }@b@ try {@b@ temp = Integer.parseInt(str.substring(dot3));@b@ if (temp < 0 || temp > 255) {@b@ return false;@b@ }@b@ } catch (Exception ex) {@b@ return false;@b@ }@b@ return true;@b@ }@b@ return false;@b@ }@b@@b@ public static boolean isNotempty(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^\\S+$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //图片校验@b@ public static boolean isPicture(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern@b@ .compile("(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //压缩包校验@b@ public static boolean isRar(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("(.*)\\.(rar|zip|7zip|tgz)$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //日期校验@b@ public static boolean isDate(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern@b@ .compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //qq校验@b@ public static boolean isQq(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^[1-9]*[1-9][0-9]*$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //电话号码校验@b@ public static boolean isTel(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern@b@ .compile("^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //用户姓名校验@b@ public static boolean isUsername(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^\\w+$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //字母校验@b@ public static boolean isLetter(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^[A-Za-z]+$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //大写字母@b@ public static boolean isLetter_u(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^[A-Z]+$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //小写字母@b@ public static boolean isLetter_l(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern.compile("^[a-z]+$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@ //价格校验@b@ public static boolean isPrice(String value) {@b@ Pattern p = null;@b@ Matcher m = null;@b@ boolean b = false;@b@ p = Pattern@b@ .compile("^([1-9]{1}[0-9]{0,}(\\.[0-9]{0,2})?|0(\\.[0-9]{0,2})?|\\.[0-9]{1,2})$");@b@ m = p.matcher(value);@b@ b = m.matches();@b@ return b;@b@ }@b@@b@ //用户账号格式判断@b@ public static boolean isUserAccount(String userAccount) {@b@ String str = "^[a-zA-Z_]{1}\\w{5,15}$";//tiansankun 描述:注册用户名格式校验修改@b@ Pattern p = Pattern.compile(str);@b@ Matcher m = p.matcher(userAccount);@b@ return m.matches();@b@ }@b@@b@ //用户密码判断@b@ public static boolean isUserPwd(String userpwd) {@b@ String str = "^[\\w-]{6,16}$";@b@ Pattern p = Pattern.compile(str);@b@ Matcher m = p.matcher(userpwd);@b@ return m.matches();@b@ }@b@@b@ //真实姓名格式判断@b@ public static boolean isUserName(String userName) {@b@ userName = userName.replaceAll(" ", "");@b@ String str = "^[\u4e00-\u9fa5]{1}[\u4e00-\u9fa5\\·]{0,48}[\u4e00-\u9fa5]{1}$";@b@ Pattern p = Pattern.compile(str);@b@ Matcher m = p.matcher(userName);@b@ return m.matches();@b@ }@b@@b@ //护照格式判断@b@ public static boolean isPassport(String passport) {@b@ passport = passport.replaceAll(" ", "");@b@ String str = "^([a-zA-Z]{5,17})|([a-zA-Z0-9]{5,17})$";@b@ Pattern p = Pattern.compile(str);@b@ Matcher m = p.matcher(passport);@b@ return m.matches();@b@ }@b@@b@ //组织机构代码@b@ public static boolean isOrganizeCode(String organizeCode) {@b@ String str = "^([0-9A-Z]){8}[-]?[0-9|X]$";@b@ Pattern p = Pattern.compile(str);@b@ Matcher m = p.matcher(organizeCode);@b@ return m.matches();@b@ }@b@@b@ //金额@b@ public static boolean isRegisteredCapital(String registeredCapital) {@b@ String str = "^(([1-9]\\d{0,11})|0)(\\.\\d{1,2})?$";@b@ Pattern p = Pattern.compile(str);@b@ Matcher m = p.matcher(registeredCapital);@b@ return m.matches();@b@ }@b@@b@ // 性别@b@ public static boolean isSex(String sex) {@b@ if (sex.equals("1") || sex.equals("2")) {@b@ return true;@b@ }@b@ return false;@b@ }@b@@b@ // 银行账号校验@b@ public static boolean isBankAccNo(String str) {@b@ if (!isNum(str)) {@b@ return false;@b@ }@b@ if (str.length() != 15 && str.length() != 16 && str.length() != 19 && str.length() != 23) {@b@ return false;@b@ }@b@@b@ return true;@b@ }@b@@b@ /*@b@ * 功能:身份证的有效验证@b@ * @param IDStr 身份证号@b@ * @return 有效:返回"" 无效:返回String信息@b@ * @throws ParseException@b@ */@b@// @SuppressWarnings("unchecked")@b@// public static String IDCardValidate(String IDStr, Date birthday, String sex) throws ParseException {@b@// String errorInfo = "";// 记录错误信息@b@// String[] ValCodeArr = {"1", "0", "x", "9", "8", "7", "6", "5", "4",@b@// "3", "2"};@b@// String[] Wi = {"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7",@b@// "9", "10", "5", "8", "4", "2"};@b@// String Ai = "";@b@// // ================ 号码的长度 15位或18位 ================@b@// if (IDStr.length() != 15 && IDStr.length() != 18) {@b@// errorInfo = "身份证号码长度应该为15位或18位。";@b@// return errorInfo;@b@// }@b@// // =======================(end)========================@b@//@b@// // ================ 数字 除最后以为都为数字 ================@b@// //性别位@b@// int gender = 0;@b@// if (IDStr.length() == 18) {@b@// Ai = IDStr.substring(0, 17);@b@// gender = Integer.parseInt(IDStr.substring(16, 17)) % 2 == 1 ? 1 : 2;@b@// } else if (IDStr.length() == 15) {@b@// gender = Integer.parseInt(IDStr.substring(14, 15)) % 2 == 1 ? 1 : 2;@b@// Ai = IDStr.substring(0, 6) + "19" + IDStr.substring(6, 15);@b@// }@b@// if (isNum(Ai) == false) {@b@// errorInfo = "身份证15位号码都应为数字 ; 18位号码除最后一位外,都应为数字。";@b@// return errorInfo;@b@// }@b@// // =======================(end)========================@b@//@b@// //性别判断@b@// if (isNotempty(sex) && !String.valueOf(gender).equals(sex)) {@b@// errorInfo = "身份证号码与性别不匹配!";@b@// return errorInfo;@b@// }@b@// // ================ 出生年月是否有效 ================@b@// String strYear = Ai.substring(6, 10);// 年份@b@// String strMonth = Ai.substring(10, 12);// 月份@b@// String strDay = Ai.substring(12, 14);// 月份@b@// if (isDate(strYear + "-" + strMonth + "-" + strDay) == false) {@b@// errorInfo = "身份证生日无效。";@b@// return errorInfo;@b@// }@b@//@b@// GregorianCalendar gc = new GregorianCalendar();@b@// SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");@b@//@b@// if (birthday != null && !(strYear + "-" + strMonth + "-" + strDay).equals(s.format(birthday))) {@b@// errorInfo = "身份证号码与出生日期不匹配!";@b@// return errorInfo;@b@// }@b@//@b@// if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150@b@// || (gc.getTime().getTime() - s.parse(@b@// strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {@b@// errorInfo = "身份证生日不在有效范围。";@b@// return errorInfo;@b@// }@b@// if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {@b@// errorInfo = "身份证月份无效";@b@// return errorInfo;@b@// }@b@// if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {@b@// errorInfo = "身份证日期无效";@b@// return errorInfo;@b@// }@b@// // =====================(end)=====================@b@//@b@// // ================ 地区码时候有效 ================@b@// Hashtable h = GetAreaCode();@b@// if (h.get(Ai.substring(0, 2)) == null) {@b@// errorInfo = "身份证地区编码错误。";@b@// return errorInfo;@b@// }@b@// // ==============================================@b@//@b@// // ================ 判断最后一位的值 ================@b@// int TotalmulAiWi = 0;@b@// for (int i = 0; i < 17; i++) {@b@// TotalmulAiWi = TotalmulAiWi@b@// + Integer.parseInt(String.valueOf(Ai.charAt(i)))@b@// * Integer.parseInt(Wi[i]);@b@// }@b@// int modValue = TotalmulAiWi % 11;@b@// String strVerifyCode = ValCodeArr[modValue];@b@// Ai = Ai + strVerifyCode;@b@//@b@// if (IDStr.length() == 18) {@b@// if (Ai.equals(IDStr.toLowerCase()) == false) {@b@// errorInfo = "身份证无效,不是合法的身份证号码";@b@// return errorInfo;@b@// }@b@// } else {@b@// return "";@b@// }@b@// // =====================(end)=====================@b@// return "";@b@// }@b@@b@ /**@b@ * 功能:设置地区编码@b@ * @return Hashtable 对象@b@ */@b@ @SuppressWarnings("unchecked")@b@ public static Hashtable GetAreaCode() {@b@ Hashtable hashtable = new Hashtable();@b@ hashtable.put("11", "北京");@b@ hashtable.put("12", "天津");@b@ hashtable.put("13", "河北");@b@ hashtable.put("14", "山西");@b@ hashtable.put("15", "内蒙古");@b@ hashtable.put("21", "辽宁");@b@ hashtable.put("22", "吉林");@b@ hashtable.put("23", "黑龙江");@b@ hashtable.put("31", "上海");@b@ hashtable.put("32", "江苏");@b@ hashtable.put("33", "浙江");@b@ hashtable.put("34", "安徽");@b@ hashtable.put("35", "福建");@b@ hashtable.put("36", "江西");@b@ hashtable.put("37", "山东");@b@ hashtable.put("41", "河南");@b@ hashtable.put("42", "湖北");@b@ hashtable.put("43", "湖南");@b@ hashtable.put("44", "广东");@b@ hashtable.put("45", "广西");@b@ hashtable.put("46", "海南");@b@ hashtable.put("50", "重庆");@b@ hashtable.put("51", "四川");@b@ hashtable.put("52", "贵州");@b@ hashtable.put("53", "云南");@b@ hashtable.put("54", "西藏");@b@ hashtable.put("61", "陕西");@b@ hashtable.put("62", "甘肃");@b@ hashtable.put("63", "青海");@b@ hashtable.put("64", "宁夏");@b@ hashtable.put("65", "新疆");@b@ hashtable.put("71", "台湾");@b@ hashtable.put("81", "香港");@b@ hashtable.put("82", "澳门");@b@ hashtable.put("91", "国外");@b@ return hashtable;@b@ }@b@@b@ /**@b@ * 校验请求分页数据@b@ *@b@ * @param requestInfo@b@ * @return@b@ */@b@// public static boolean verifyReqPageInfo(RequestInfo requestInfo) {@b@// Integer currentPage = requestInfo.getCurrentPage();@b@// if (currentPage < 0) {@b@// setVerifyPageError("当前页码不能小于零,原因:当前页码为" + currentPage);@b@// return false;@b@// }@b@// Integer pageSize = requestInfo.getPageSize();@b@// if (pageSize <= 0) {@b@// setVerifyPageError("设置当前页行数不能小于或等于零,原因:当前页行数为" + pageSize);@b@// return false;@b@// }@b@// return true;@b@// }@b@@b@@b@ /**@b@ * 输出详细日志@b@ *@b@ * @param anexcepObj@b@ * @return@b@ */@b@ public static String getExceptionStackTrace(Throwable anexcepObj) {@b@ StringWriter sw = null;@b@ PrintWriter printWriter = null;@b@ try {@b@ if (anexcepObj != null) {@b@ sw = new StringWriter();@b@ printWriter = new PrintWriter(sw);@b@ anexcepObj.printStackTrace(printWriter);@b@ printWriter.flush();@b@ sw.flush();@b@ return sw.toString();@b@ } else@b@ return null;@b@ } finally {@b@@b@ try {@b@ if (sw != null)@b@ sw.close();@b@ if (printWriter != null)@b@ printWriter.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@@b@ }@b@@b@}