package org.fh.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 說明:常用工具 * 作者:FH Admin * from:fhadmin.cn */ public class Tools { /** * 隨機生成六位數驗證碼 * @return */ public static int getRandomNum(){ Random r = new Random(); return r.nextInt(900000)+100000;//(Math.random()*(999999-100000)+100000) } /** * 隨機生成四位數驗證碼 * @return */ public static int getRandomNum4(){ Random r = new Random(); return r.nextInt(9000)+1000; } /** * 隨機生成兩位數驗證碼 * @return */ public static int getRandomNum2(){ Random r = new Random(); return r.nextInt(90)+10; } /** * 檢測字串是否不為空(null,"","null") * @param s * @return 不為空則回傳true,否則回傳false */ public static boolean notEmpty(String s){ return s!=null && !"".equals(s) && !"null".equals(s); } /** * 檢測字串是否為空(null,"","null") * @param s * @return 為空則回傳true,不否則回傳false */ public static boolean isEmpty(String s){ return s==null || "".equals(s) || "null".equals(s); } /** * 字串轉換為字串陣列 * @param str 字串 * @param splitRegex 分隔符 * @return */ public static String[] str2StrArray(String str,String splitRegex){ if(isEmpty(str)){ return null; } return str.split(splitRegex); } /** * 用默認的分隔符(,)將字串轉換為字串陣列 * @param str 字串 * @return */ public static String[] str2StrArray(String str){ return str2StrArray(str,",\\s*"); } /** * 往檔案里的內容 * @param filePath 檔案路徑 * @param content 寫入的內容 */ public static void writeFile(String fileP,String content){ String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"../../"; //專案路徑 filePath = filePath.replaceAll("file:/", ""); filePath = filePath.replaceAll("%20", " "); filePath = filePath.trim() + fileP.trim(); if(filePath.indexOf(":") != 1){ filePath = File.separator + filePath; } try { OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(filePath),"utf-8"); BufferedWriter writer=new BufferedWriter(write); writer.write(content); writer.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 往檔案里的內容(Projectpath下) * @param filePath 檔案路徑 * @param content 寫入的內容 */ public static void writeFileCR(String fileP,String content){ String filePath = PathUtil.getProjectpath() + fileP; try { OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(filePath),"utf-8"); BufferedWriter writer=new BufferedWriter(write); writer.write(content); writer.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 驗證郵箱 * @param email * @return */ public static boolean checkEmail(String email){ boolean flag = false; try{ String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; Pattern regex = Pattern.compile(check); Matcher matcher = regex.matcher(email); flag = matcher.matches(); }catch(Exception e){ flag = false; } return flag; } /** * 驗證手機號碼 * @param mobiles * @return */ public static boolean checkMobileNumber(String mobileNumber){ boolean flag = false; try{ Pattern regex = Pattern.compile("^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$"); Matcher matcher = regex.matcher(mobileNumber); flag = matcher.matches(); }catch(Exception e){ flag = false; } return flag; } /** * 檢測KEY是否正確 * @param paraname 傳入引數 * @param FKEY 接收的 KEY * @return 為空則回傳true,不否則回傳false */ public static boolean checkKey(String paraname, String FKEY){ paraname = (null == paraname)? "":paraname; return MD5.md5(paraname+DateUtil.getDays()+",fh,").equals(FKEY); } /**讀取txt里的全部內容 * @param fileP 檔案路徑 * @param encoding 編碼 * @return */ public static String readTxtFileAll(String fileP, String encoding) { StringBuffer fileContent = new StringBuffer(); try { String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"../../"; //專案路徑 filePath = filePath.replaceAll("file:/", ""); filePath = filePath.replaceAll("%20", " "); filePath = filePath.trim() + fileP.trim(); if(filePath.indexOf(":") != 1){ filePath = File.separator + filePath; } File file = new File(filePath); if (file.isFile() && file.exists()) { // 判斷檔案是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file), encoding); // 考慮到編碼格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { fileContent.append(lineTxt); fileContent.append("\n"); } read.close(); }else{ System.out.println("找不到指定的檔案,查看此路徑是否正確:"+filePath); } } catch (Exception e) { System.out.println("讀取檔案內容出錯"); } return fileContent.toString(); } /** * 讀取Projectpath某檔案里的全部內容 * @param filePath 檔案路徑 */ public static String readFileAllContent(String fileP) { StringBuffer fileContent = new StringBuffer(); try { String encoding = "utf-8"; File file = new File(PathUtil.getProjectpath() + fileP);//檔案路徑 if (file.isFile() && file.exists()) { // 判斷檔案是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file), encoding); // 考慮到編碼格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { fileContent.append(lineTxt); fileContent.append("\n"); } read.close(); }else{ System.out.println("找不到指定的檔案,查看此路徑是否正確:"+fileP); } } catch (Exception e) { System.out.println("讀取檔案內容出錯"); } return fileContent.toString(); } public static void main(String[] args) { System.out.println(getRandomNum()); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/443441.html
標籤:Java
