超强的封装工具-------HUTOOL 工具!!!!

hutool java封装工具 网址https://www.hutool.cn/docs/#/poi/Word%E7%94%9F%E6%88%90-Word07Writer

我在本次的博客中介绍20例子.更多的例子可以访问网站官网.

1.准备依赖

	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>5.7.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>4.1.2</version>
		</dependency>
		<dependency>
			<groupId>com.vdurmont</groupId>
			<artifactId>emoji-java</artifactId>
			<version>4.0.0</version>
		</dependency>

		<dependency>
			<groupId>com.sun.mail</groupId>
			<artifactId>javax.mail</artifactId>
			<version>1.6.2</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

2.代码部分

@SpringBootTest
class HutoolTestApplicationTests {

	@Test
	void contextLoads() throws IOException {

		/**hutool:ResourceUtil 资源工具
		 * */

		//读取文件
		String str = ResourceUtil.readUtf8Str("q.txt");
		System.out.println(str);

        //读取配置文件
		ClassPathResource resource = new ClassPathResource("application.properties");
		Properties prop = new Properties();
		prop.load(resource.getStream());
		Console.log("propperties: {}",prop);

	}


	@Test
	void contextLoads1() {

		/**hutool:DateUtil 时间工具
		 * */

        //当前时间
		DateTime date = DateUtil.date();
		Console.log("date:",date);

		//当前时间
		DateTime date1 = DateUtil.date(Calendar.getInstance());
		Console.log("date:",date1);

		//当前时间
		DateTime date2 = DateUtil.date(System.currentTimeMillis());
		Console.log("date:",date2);

		//当前时间字符串,格式:yyyy-MM-dd HH:mm:ss
		String now = DateUtil.now();
		Console.log("date:",now);


        //当前日期字符串,格式:yyyy-MM-dd
		String today= DateUtil.today();
		Console.log("date:",today);



		//常用格式的格式化,2021250933591
		String format = DateUtil.format(date, "yyyymmddssSS");
		Console.log("date:",format);

		//计时工具
		TimeInterval timer = DateUtil.timer();

		long interval = timer.interval();
		Console.log("interval:",interval);

		long minute = timer.intervalMinute();
		Console.log("minute:",minute);

	}



	@Test
	void contextLoads3() throws IOException {

		/**hutool: IoUtil 拷贝工具
		 * */

		BufferedInputStream inputStream = FileUtil.getInputStream("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\q.txt");
		BufferedOutputStream outputStream = FileUtil.getOutputStream("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\a.txt");

		//默认:覆盖
		long copy = IoUtil.copy(inputStream, outputStream, IoUtil.EOF);

		System.out.println(copy);


	}

	@Test
	void contextLoads4() throws IOException {

		/**hutool: FileUtil 文件工具
		 *
		 * */
		//列出目录
		File[] ls = FileUtil.ls("D:\\ideaProject\\hutoolTest");
		for (File l : ls) {
			System.out.println(l);
		}

		//创建文件
		File touch = FileUtil.touch("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\config1\\c.txt");
		touch.canRead();
		touch.canWrite();

        //mkdir 创建目录
		File mkdir = FileUtil.mkdir("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\config1");
		mkdir.canWrite();
		mkdir.canRead();


       //del 递归删除
		boolean del = FileUtil.del("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\config1\\c.txt");
		System.out.println(del);


	}

	@Test
	void contextLoads5() throws IOException {

		/**hutool: FileTypeUtil 文件类型工具
		 * */
		File file = FileUtil.file("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\a.txt");
		String type = FileTypeUtil.getType(file);
		System.out.println(type);

		String xml = FileTypeUtil.putFileType("15f7", "xml");

	}


	@Test
	void contextLoads6() throws IOException {

		/**hutool: 16进制工具-HexUtil
		 * */
		String str = "我是一个字符串";

		String hex = HexUtil.encodeHexStr(str, CharsetUtil.CHARSET_UTF_8);
		Console.log("hex:",hex);

	}


	@Test
	void contextLoads7() throws IOException {

		/**
		 * hutool: XML工具-XmlUtil
		 * */
		Document document = XmlUtil.readXML("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\c.xml");
		System.out.println(document);

		Object byXPath = XmlUtil.getByXPath("//returnsms/message", document, XPathConstants.STRING);
		System.out.println(byXPath);
	}

	@Test
	void contextLoads8() throws Exception {

		/**
		 * hutool: 命令行工具-RuntimeUtil
		 * */

		String ipconfig = RuntimeUtil.execForStr("ipconfig");
		System.out.println(ipconfig);
		List<String> ipconfig1 = RuntimeUtil.execForLines("ipconfig");
		System.out.println(ipconfig1);


	}


	@Test
	void contextLoads9() throws Exception {

		/**
		 * hutool: 随机工具-RandomUtil
		 * */

		//获得指定范围内的随机数
		int i = RandomUtil.randomInt(1000);
		System.out.println(i);

        //获得一个只包含数字的字符串
		String s = RandomUtil.randomNumbers(10);
		System.out.println(s);

		// 随机UUID 暂时不能使用
       // RandomUtil.randomUUID()

	}


    @Test
    void contextLoads10() throws Exception {

        /**
         * hutool: 唯一ID工具-IdUtil
         * */

        //生成的UUID是带-的字符串
        String uuid = IdUtil.randomUUID();
        System.out.println(uuid);

        //生成的是不带-的字符串
        String simpleUUID = IdUtil.simpleUUID();
        System.out.println(simpleUUID);


        //MongoDB使用
        String objectId = IdUtil.objectId();
        System.out.println(objectId);
    }

    @Test
    void contextLoads11() throws Exception {

        /**
         * hutool: 身份证工具-IdcardUtil
         * */

        String ID_18="321083197812162119";
        String ID_15 = "150102880730303";
        //验证合法性
        boolean validCard = IdcardUtil.isValidCard(ID_18);
        boolean validCard1 = IdcardUtil.isValidCard(ID_15);
        System.out.println(validCard);
        System.out.println(validCard1);

        //获取性别
        int genderByIdCard = IdcardUtil.getGenderByIdCard(ID_18);
        System.out.println(genderByIdCard);
        //获取省份
        String provinceByIdCard = IdcardUtil.getProvinceByIdCard(ID_18);
        System.out.println(provinceByIdCard);

        //年龄
        DateTime date = DateUtil.parse("2021-08-09");
        int ageByIdCard = IdcardUtil.getAgeByIdCard(ID_18, date);
        System.out.println(ageByIdCard);

        //生日
        String birth = IdcardUtil.getBirthByIdCard(ID_18);
        System.out.println(birth);

    }


    @Test
    void contextLoads12() throws Exception {

        /**
         * hutool: 信息脱敏工具-DesensitizedUtil
         * */
        //身份证
        String s = DesensitizedUtil.idCardNum("51343620000320711X", 1, 2);
        System.out.println(s);
        //手机号
		String s1 = DesensitizedUtil.mobilePhone("13566901251");
		System.out.println(s1);

		//中文名字
		String s2 = DesensitizedUtil.chineseName("张三");
		System.out.println(s2);

		//邮箱
		String email = DesensitizedUtil.email("qrw1314@163.com");
		System.out.println(email);

	}



	@Test
	void contextLoads13() throws Exception {

		/**
		 * hutool: HashMap扩展-Dict(Python字典)
		 * */

		Dict dict = Dict.create()
				.set("key1", 1)//int
				.set("key2", 1000L)//long
				.set("key3", DateTime.now());

		System.out.println(dict);
	}

	@Test
	void contextLoads14() throws Exception {

		/**
		 * hutool: 控制台打印封装-Console
		 * */

		String[] a = {"abc", "bcd", "def"};
		Console.log(a);
	}


	@Test
	void contextLoads15() throws Exception {

		/**
		 * hutool: 邮件工具-MailUtil
		 * 注意:AuthenticationFailedException: 550 User has no permission
		 * */
		MailUtil.send("hutool@foxmail.com", "测试", "邮件来自Hutool测试", false);

	}

	@Test
	void contextLoads16() throws Exception {

		/**
		 * hutool: Excel工具-ExcelUtil
		 *注意:在写入时候一定关闭文件
		 * */

		ExcelReader reader = ExcelUtil.getReader(FileUtil.file("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\1.xlsx"));
		List<List<Object>> read = reader.read();
		for (List<Object> objects : read) {

			System.out.println(objects);
		}
		Map<String, Object> row1 = new LinkedHashMap<>();

		row1.put("id",1);
		row1.put("name","xiaolanlan");
		row1.put("age",34);
		row1.put("addree","shanghai");


		Map<String, Object> row2 = new LinkedHashMap<>();

		row2.put("id",1);
		row2.put("name","xiaolanlan");
		row2.put("age",34);
		row2.put("addree","shanghai");

		ArrayList<Map<String, Object>> maps = CollUtil.newArrayList(row1, row2,row1,row2);
		ExcelWriter writer = ExcelUtil.getWriter("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\1.xlsx");

		writer.write(maps,true);
		writer.close();
	}



	@Test
	void contextLoads17() throws Exception {

		/**
		 * hutool: 系统属性调用-SystemUtil
		 *
		 * */

		JvmSpecInfo info = SystemUtil.getJvmSpecInfo();
		System.out.println(info);

		JvmInfo jvmInfo = SystemUtil.getJvmInfo();
		System.out.println(jvmInfo);

		JavaRuntimeInfo javaRuntimeInfo = SystemUtil.getJavaRuntimeInfo();
		System.out.println(javaRuntimeInfo);


		RuntimeInfo runtimeInfo = SystemUtil.getRuntimeInfo();
		System.out.println(runtimeInfo);

		HostInfo hostInfo = SystemUtil.getHostInfo();
		System.out.println(hostInfo);


	}


	@Test
	void contextLoads18() throws Exception {

		/**
		 * hutool: Emoji工具-EmojiUtil
		 * */
		String s = EmojiUtil.toAlias("\uD83D\uDE04");
		System.out.println(s);


		String emoji = EmojiUtil.toUnicode(":smile:");
		System.out.println(emoji);


	}


	@Test
	void contextLoads19() throws Exception {

		/**
		 * hutool: Script工具-ScriptUtil
		 * */
		Object eval = ScriptUtil.eval("print('Script test!');");
		System.out.println(eval);


		CompiledScript script = ScriptUtil.compile("print('Script test!');");
		Object eval1 = script.eval();
	}


	@Test
	void contextLoads20() throws Exception {

		/**
		 * hutool: Word生成-Word07Writer
		 * */
		Word07Writer writer = new Word07Writer();

		 writer.addText(new Font("方正小标宋简体", Font.PLAIN, 22), "我是第一部分", "我是第二部分");
		 writer.addText(new Font("宋体", Font.PLAIN, 22), "我是正文第一部分", "我是正文第二部分");
		 writer.flush(FileUtil.file("D:\\ideaProject\\hutoolTest\\src\\main\\resources\\wordWrite.docx"));
		 writer.close();
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: hutool-all-5.7.20.jar是一个Java编写的工具类库,它提供了丰富的工具方法、函数和静态常量,帮助开发者们高效地开发Java应用程序。它的具体特点包括: 1. 提供了基础型数据类型之间的转换、字符串、集合、日期、IO等常用工具封装。 2. 提供了高效便捷的文件操作,包括读、写、复制、移动、压缩、解压等功能。 3. 提供了快速生成二维码和条码等功能。 4. 提供了XML文件解析、JSON转换等常用数据格式转换的封装。 5. 提供了加密、解密、摘要等常用加密算法封装,包括MD5、SHA、AES、DES等。 6. 提供了爬虫、定时任务等高级功能的支持,可以帮助开发者更方便地处理复杂业务逻辑。 总之,hutool-all-5.7.20.jar是一个非常优秀的Java工具类库,它能够极大地提高Java应用程序的开发效率,为开发者们带来更多的便捷和支持。 ### 回答2: hutool-all-5.7.20.jar是一个开源的Java工具库,主要用于简化Java开发中的一些常见任务。它包含丰富的工具类,如字符串工具类、集合工具类、IO工具类、日期工具类等等,可以让开发人员更加便捷地完成开发任务。 hutool-all-5.7.20.jar的优点很多,首先它提供了很多高效的工具类,这些工具类可以帮助开发人员节省很多时间和精力。其次,这个Java工具库非常易于使用,开发人员可以轻松地将其集成到自己的项目中,而且不需要学习很多复杂的API和语法。 此外,hutool-all-5.7.20.jar还提供了很多实用的功能,如JSON解析、加密解密、文件压缩、Excel处理等等,这些功能可以让开发人员更加方便地完成各种开发任务。最后,这个Java工具库还拥有非常活跃的社区支持,开发人员可以通过社区获得帮助和指导。 总之,hutool-all-5.7.20.jar是一个非常实用的Java工具库,它可以帮助开发人员更加高效地完成开发任务。如果你是Java开发人员,那么强烈建议你使用这个工具库,相信它一定能够大大提升你的开发效率。 ### 回答3: hutool-all-5.7.20.jar是一款Java工具类库,提供大量封装好的工具类、接口和方法,包括字符串操作、日期时间处理、加密解密、文件操作、网络通信、HTML解析等。hutool-all-5.7.20.jar底层基于jdk封装,采用Apache License 2.0协议,使用和转载完全免费。 hutool-all-5.7.20.jar提供了许多常用的工具类,让开发者可以快速高效地完成各种操作,例如:StrUtil类中提供了许多字符串操作的方法,如判断是否为空、是否为null、是否为空白字符等;DateUtil类中提供了格式化、解析、计算时间差等方法,非常适合处理日期时间;SecureUtil类中提供了常见的加密算法,如md5、SHA-1等;FileUtil类中提供了常用的文件操作方法,如创建文件、删除文件、复制文件等;EmailUtil、HttpUtil等类则为网络通信提供了方便的方法等等。 由于hutool-all-5.7.20.jar提供了大量丰富的工具类和方法,使得开发人员可以快速编写出高质量、高效的Java程序,提高生产力和开发速度,受到了广大Java开发者的推崇。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值