java 常用工具类 (值得收藏)

Posted fhspringcloud

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 常用工具类 (值得收藏)相关的知识,希望对你有一定的参考价值。

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]))\\\\d8)|(0\\\\d2-\\\\d8)|(0\\\\d3-\\\\d7)$");
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());



以上是关于java 常用工具类 (值得收藏)的主要内容,如果未能解决你的问题,请参考以下文章

最值得收藏的 eclipse快捷键(java) 常用快捷键使用, 并和不同软件中相同快捷键作比较, 让你的效率成倍增加

最值得收藏的 搜狗输入法 常用快捷键使用, 让你的效率成倍增加

springboot整合Hutool实现Convert类型转换实用案例-值得收藏

最值得Java开发者收藏的网站

10 个问题搞定 Java 异常处理,值得收藏!

深入Java类加载全流程,值得你收藏