Java tutorial
/* * Copyright 2015-9999 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.beginner.core.utils; 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.Collection; import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; /** * <b>??</b>Tools<br/> * <b>??</b><br/> * <b></b>Hsiao Lin Studio<br/> * <b></b><br/> * <b></b>20150521 ?6:18:18<br/> * <b></b><br/> * @version 1.0.0<br/> */ public class Tools { /** * ????? * @return int ??? * @since 1.0.0 */ public static int getRandomNum() { Random r = new Random(); return r.nextInt(900000) + 100000; } /** * ?(null,"","null","NULL","Null") * @param str ?? * @return boolean true?false * @since 1.0.0 */ public static boolean isEmpty(String str) { return StringUtils.isBlank(str) || "null".equalsIgnoreCase(str); } /** * ?? * @param collection ??? * @return boolean true?false * @since 1.0.0 */ public static boolean isEmpty(Collection<?> collection) { return CollectionUtils.isEmpty(collection); } /** * ??(null,"","null") * @param str ?? * @return boolean true?false * @since 1.0.0 */ public static boolean isNotEmpty(String str) { return StringUtils.isNotBlank(str) && !"null".equalsIgnoreCase(str); } /** * ??? * @param collection ??? * @return boolean true?false * @since 1.0.0 */ public static boolean isNotEmpty(Collection<?> collection) { return CollectionUtils.isNotEmpty(collection); } /** * ? * @param str ? * @param splitRegex * @return String[] * @since 1.0.0 */ public static String[] str2Array(String str, String splitRegex) { if (isEmpty(str)) { return null; } return str.split(splitRegex); } /** * (,)? * @param str ? * @return String[] * @since 1.0.0 */ public static String[] str2Array(String str) { return str2Array(str, ",\\s*"); } /** * ? * @param email * @return boolean true?false * @since 1.0.0 */ 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 mobileNumber ?? * @return boolean true?false * @since 1.0.0 */ 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; } /** * ?txt? * @param fileP * @param content * @since 1.0.0 */ public static void writeFile(String fileP, String content) { String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) + "../../"; // filePath = (filePath.trim() + fileP.trim()).substring(6).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(); } } /** * ?txt? * @param filepath * @return String * @since 1.0.0 */ public static String readTxtFile(String path) { try { String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) + "../../"; // filePath = filePath.replaceAll("file:/", ""); filePath = filePath.replaceAll("%20", " "); filePath = filePath.trim() + path.trim(); if (filePath.indexOf(":") != 1) { filePath = File.separator + filePath; } String encoding = "utf-8"; 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) { return lineTxt; } read.close(); } else { System.out.println("?,?:" + filePath); } } catch (Exception e) { System.out.println("?"); } return ""; } }