Huool常用工具类

Hutool中的工具类很多,可以参考:Hutool — A set of tools that keep Java sweet.

以下是若依cloud plus版本中使用过的Hutool工具

package org.dromara.hutool;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Editor;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import com.esotericsoftware.minlog.Log;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.*;

@Slf4j
public class HutoolLearn {

    public static void main(String[] args) {
        ioUtil();
        //objectUtil();
        //htmlUtil();
        //reUtil();
        //strUtil();
        //validator();
        //convert();
        //collUtil();
        //arrayUtil();
        //SpringUtil  参见SpringUtils
        //HttpStatus
        //beanUtil();
        netUtil();

    }

    //IO
    public static void ioUtil() {
        File src = new File("C:\\test1.txt");
        FileReader fr = null;
        InputStream input = null;
        try {
            //创建流
            fr = new FileReader(src);
            //input = new FileInputStream(src);
            input = FileUtil.getInputStream(src);
            //ArrayList readLines = IoUtil.readLines(fr, new ArrayList<>());
            //从流中读取内容,使用UTF-8编码
            //ArrayList readLines = IoUtil.readUtf8Lines(input, new ArrayList<>());
            ArrayList readLines = IoUtil.readLines(input, "UTF-8", new ArrayList<>());
            for (String readLine : readLines) {
                System.out.println(readLine);
            }
        } catch (IOException e) {
            //抛出一个运行时异常(直接停止掉程序)
            throw new RuntimeException("运行时异常", e);
        } finally {
            try {
                //如果是空的 说明流创建失败 失败了不需要关闭
                if (fr != null) {
                    fr.close();
                }
            } catch (Exception e) {
                //关闭资源失败 停止程序
                throw new RuntimeException("关闭资源失败");
            }
        }

    }

    public static void objectUtil() {
        // 定义一个集合null;
        List a = null;
        System.out.println("定义一个集合null:" + ObjectUtil.isEmpty(a) + "--" + ObjectUtil.isNull(a));
        // 定义一个集合为空;
        List b = new ArrayList<>();
        System.out.println("定义一个集合为空:" + ObjectUtil.isEmpty(b) + "--" + ObjectUtil.isNull(b));
    }

    public static void htmlUtil() {
        //检测是否包含html标记


        String escape = HtmlUtil.escape("
这是一段html
"); System.out.println(escape); System.out.println(HtmlUtil.unescape(escape)); } public static void reUtil() { //正则表达式,验证是否包含html标记 boolean value = ReUtil.contains(HtmlUtil.RE_HTML_MARK, "
这是一段html
"); System.out.println(value); } public static void strUtil() { //判断是否为空字符串 String str = "test"; System.out.println(StrUtil.isEmpty(str)); System.out.println(StrUtil.isNotEmpty(str)); //去除字符串的前后缀 System.out.println(StrUtil.removeSuffix("a.jpg", ".jpg")); System.out.println(StrUtil.removePrefix("a.jpg", "a.")); //格式化字符串 String template = "这只是个占位符:{}"; String str2 = StrUtil.format(template, "我是占位符"); System.out.println("strUtil format:" + str2); } public static void validator() { System.out.println(Validator.isUrl("http://www.baidu.com")); System.out.println(Validator.isEmail("[email protected]")); System.out.println(Validator.isLowerCase("Abc")); } public static void convert() { //转换为字符串 int a = 1; String aStr = Convert.toStr(a); //转换为指定类型数组 String[] b = {"1", "2", "3", "4"}; Integer[] bArr = Convert.toIntArray(b); //转换为日期对象 String dateStr = "2017-05-06"; Date date = Convert.toDate(dateStr); //转换为列表 String[] strArr = {"a", "b", "c", "d"}; List strList = Convert.toList(String.class, strArr); } /** * collUtil */ public static void collUtil(){ //数组转换为列表 String[] array = new String[]{"a", "b", "c", "d", "e"}; List list = CollUtil.newArrayList(array); //join:数组转字符串时添加连接符号 String joinStr = CollUtil.join(list, ","); System.out.println(joinStr); //将以连接符号分隔的字符串再转换为列表 List splitList = StrUtil.split(joinStr, ','); System.out.println(splitList); //创建新的Map、Set、List //HashMap newMap = CollUtil.newHashMap(); HashSet newHashSet = CollUtil.newHashSet(); ArrayList newList = CollUtil.newArrayList(); //判断列表是否为空 CollUtil.isEmpty(list); } public static void arrayUtil(){ String[] newArray = ArrayUtil.newArray(String.class, 3); String[] sta = {"1","2"}; ArrayUtil.isNotEmpty(sta); } public static void mapUtil(){ HashMap newMap =MapUtil.newHashMap(); MapUtil.isEmpty(newMap); MapUtil.isNotEmpty(newMap); } public static void beanUtil(){ PmsBrand brand = new PmsBrand(); brand.setId(1L); brand.setName("小米"); brand.setShowStatus(0); //Bean转Map Map map = BeanUtil.beanToMap(brand); System.out.println(("beanUtil bean to map:" + map)); //Map转Bean PmsBrand mapBrand = BeanUtil.mapToBean(map, PmsBrand.class, false); System.out.println(("beanUtil map to bean:" + mapBrand)); //Bean属性拷贝 PmsBrand copyBrand = new PmsBrand(); BeanUtil.copyProperties(brand, copyBrand); System.out.println(("beanUtil copy properties:" + copyBrand)); } public static void netUtil(){ System.out.println(NetUtil.isInnerIP("192.168.0.60")); } }

你可能感兴趣的:(windows)