Chinaunix首页 | 论坛 | 博客
  • 博客访问: 603796
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-03-14 10:36:16


点击(此处)折叠或打开


  1. package org.fh.util;

  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.io.OutputStreamWriter;
  10. import java.util.Random;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;

  13. /**
  14.  * 说明:常用工具
  15.  * 作者:FH Admin
  16.  * from:fhadmin.cn
  17.  */
  18. public class Tools {
  19.     
  20.     /**
  21.      * 随机生成六位数验证码
  22.      * @return
  23.      */
  24.     public static int getRandomNum(){
  25.          Random r = new Random();
  26.          return r.nextInt(900000)+100000;//(Math.random()*(999999-100000)+100000)
  27.     }
  28.     
  29.     /**
  30.      * 随机生成四位数验证码
  31.      * @return
  32.      */
  33.     public static int getRandomNum4(){
  34.          Random r = new Random();
  35.          return r.nextInt(9000)+1000;
  36.     }
  37.     
  38.     /**
  39.      * 随机生成两位数验证码
  40.      * @return
  41.      */
  42.     public static int getRandomNum2(){
  43.          Random r = new Random();
  44.          return r.nextInt(90)+10;
  45.     }
  46.     
  47.     /**
  48.      * 检测字符串是否不为空(null,"","null")
  49.      * @param s
  50.      * @return 不为空则返回true,否则返回false
  51.      */
  52.     public static boolean notEmpty(String s){
  53.         return s!=null && !"".equals(s) && !"null".equals(s);
  54.     }
  55.     
  56.     /**
  57.      * 检测字符串是否为空(null,"","null")
  58.      * @param s
  59.      * @return 为空则返回true,不否则返回false
  60.      */
  61.     public static boolean isEmpty(String s){
  62.         return s==null || "".equals(s) || "null".equals(s);
  63.     }
  64.     
  65.     /**
  66.      * 字符串转换为字符串数组
  67.      * @param str 字符串
  68.      * @param splitRegex 分隔符
  69.      * @return
  70.      */
  71.     public static String[] str2StrArray(String str,String splitRegex){
  72.         if(isEmpty(str)){
  73.             return null;
  74.         }
  75.         return str.split(splitRegex);
  76.     }
  77.     
  78.     /**
  79.      * 用默认的分隔符(,)将字符串转换为字符串数组
  80.      * @param str    字符串
  81.      * @return
  82.      */
  83.     public static String[] str2StrArray(String str){
  84.         return str2StrArray(str,",\\s*");
  85.     }
  86.     
  87.     /**
  88.      * 往文件里的内容
  89.      * @param filePath 文件路径
  90.      * @param content 写入的内容
  91.      */
  92.     public static void writeFile(String fileP,String content){
  93.         String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"../../";    //项目路径
  94.         filePath = filePath.replaceAll("file:/", "");
  95.         filePath = filePath.replaceAll("%20", " ");
  96.         filePath = filePath.trim() + fileP.trim();
  97.         if(filePath.indexOf(":") != 1){
  98.             filePath = File.separator + filePath;
  99.         }
  100.         try {
  101.      OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(filePath),"utf-8");
  102.      BufferedWriter writer=new BufferedWriter(write);
  103.      writer.write(content);
  104.      writer.close();
  105.         } catch (IOException e) {
  106.             e.printStackTrace();
  107.         }
  108.     }
  109.     
  110.     /**
  111.      * 往文件里的内容(Projectpath下)
  112.      * @param filePath 文件路径
  113.      * @param content 写入的内容
  114.      */
  115.     public static void writeFileCR(String fileP,String content){
  116.         String filePath = PathUtil.getProjectpath() + fileP;
  117.         try {
  118.      OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(filePath),"utf-8");
  119.      BufferedWriter writer=new BufferedWriter(write);
  120.      writer.write(content);
  121.      writer.close();
  122.         } catch (IOException e) {
  123.             e.printStackTrace();
  124.         }
  125.     }
  126.     
  127.     /**
  128.      * 验证邮箱
  129.      * @param email
  130.      * @return
  131.      */
  132.      public static boolean checkEmail(String email){
  133.      boolean flag = false;
  134.      try{
  135.      String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
  136.      Pattern regex = Pattern.compile(check);
  137.      Matcher matcher = regex.matcher(email);
  138.      flag = matcher.matches();
  139.      }catch(Exception e){
  140.      flag = false;
  141.      }
  142.      return flag;
  143.      }
  144.     
  145.      /**
  146.      * 验证手机号码
  147.      * @param mobiles
  148.      * @return
  149.      */
  150.      public static boolean checkMobileNumber(String mobileNumber){
  151.      boolean flag = false;
  152.      try{
  153.      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})$");
  154.      Matcher matcher = regex.matcher(mobileNumber);
  155.      flag = matcher.matches();
  156.      }catch(Exception e){
  157.      flag = false;
  158.      }
  159.      return flag;
  160.      }
  161.     
  162.     /**
  163.      * 检测KEY是否正确
  164.      * @param paraname 传入参数
  165.      * @param FKEY        接收的 KEY
  166.      * @return 为空则返回true,不否则返回false
  167.      */
  168.     public static boolean checkKey(String paraname, String FKEY){
  169.         paraname = (null == paraname)? "":paraname;
  170.         return MD5.md5(paraname+DateUtil.getDays()+",fh,").equals(FKEY);
  171.     }
  172.     
  173.     /**读取txt里的全部内容
  174.      * @param fileP 文件路径
  175.      * @param encoding 编码
  176.      * @return
  177.      */
  178.     public static String readTxtFileAll(String fileP, String encoding) {
  179.         StringBuffer fileContent = new StringBuffer();
  180.         try {
  181.             String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"../../";    //项目路径
  182.             filePath = filePath.replaceAll("file:/", "");
  183.             filePath = filePath.replaceAll("%20", " ");
  184.             filePath = filePath.trim() + fileP.trim();
  185.             if(filePath.indexOf(":") != 1){
  186.                 filePath = File.separator + filePath;
  187.             }
  188.             File file = new File(filePath);
  189.             if (file.isFile() && file.exists()) {         // 判断文件是否存在
  190.                 InputStreamReader read = new InputStreamReader(
  191.                 new FileInputStream(file), encoding);    // 考虑到编码格式
  192.                 BufferedReader bufferedReader = new BufferedReader(read);
  193.                 String lineTxt = null;
  194.                 while ((lineTxt = bufferedReader.readLine()) != null) {
  195.                     fileContent.append(lineTxt);
  196.                     fileContent.append("\n");
  197.                 }
  198.                 read.close();
  199.             }else{
  200.                 System.out.println("找不到指定的文件,查看此路径是否正确:"+filePath);
  201.             }
  202.         } catch (Exception e) {
  203.             System.out.println("读取文件内容出错");
  204.         }
  205.         return fileContent.toString();
  206.     }
  207.     
  208.     /**
  209.      * 读取Projectpath某文件里的全部内容
  210.      * @param filePath 文件路径
  211.      */
  212.     public static String readFileAllContent(String fileP) {
  213.         StringBuffer fileContent = new StringBuffer();
  214.         try {
  215.             String encoding = "utf-8";
  216.             File file = new File(PathUtil.getProjectpath() + fileP);//文件路径
  217.             if (file.isFile() && file.exists()) {         // 判断文件是否存在
  218.                 InputStreamReader read = new InputStreamReader(
  219.                 new FileInputStream(file), encoding);    // 考虑到编码格式
  220.                 BufferedReader bufferedReader = new BufferedReader(read);
  221.                 String lineTxt = null;
  222.                 while ((lineTxt = bufferedReader.readLine()) != null) {
  223.                     fileContent.append(lineTxt);
  224.                     fileContent.append("\n");
  225.                 }
  226.                 read.close();
  227.             }else{
  228.                 System.out.println("找不到指定的文件,查看此路径是否正确:"+fileP);
  229.             }
  230.         } catch (Exception e) {
  231.             System.out.println("读取文件内容出错");
  232.         }
  233.         return fileContent.toString();
  234.     }
  235.     
  236.     public static void main(String[] args) {
  237.         System.out.println(getRandomNum());
  238.     }
  239.     
  240. }


阅读(568) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~