Hutool簡介和常用類

Hutool簡介

Hutool是一個小而全的Java工具類庫,通過靜態方法封裝,降低相關API的學習成本,提高工作效率,使Java擁有函數式語言般的優雅,讓Java語言也可以“甜甜的”。

Hutool中的工具方法來自每個用戶的精雕細琢,它涵蓋了Java開發底層代碼中的方方面面,它既是大型項目開發中解決小問題的利器,也是小型項目中的效率擔當;

Hutool是項目中“util”包友好的替代,它節省了開發人員對項目中公用類和公用工具方法的封裝時間,使開發專注于業務,同時可以最大限度的避免封裝不完善帶來的bug。

DateUtil

下面是Hutool庫中DateUtil類的常用方法整理在一起:

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;public class DateUtilExample {public static void main(String[] args) {// 格式化日期時間Date date = new Date();String formatDateTime = DateUtil.formatDateTime(date);// 字符串轉日期String dateStr = "2024-02-21";Date parseDate = DateUtil.parse(dateStr);// 計算日期差long betweenDay = DateUtil.between(new Date(), DateUtil.offsetDay(new Date(), 1), DateUnit.DAY);// 日期偏移Date offsetDate = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, 1);// 日期比較boolean isSameDay = DateUtil.isSameDay(new Date(), new Date());// 日期時間加減Date newDate = DateUtil.offsetDay(new Date(), 1);// 日期時間格式化String formattedDate = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");// 獲取當前時間Date now = DateUtil.date();// 日期范圍判斷boolean isInThisWeek = DateUtil.isInThisWeek(new Date());}
}

上述代碼展示了Hutool庫中DateUtil類的常用方法并整理在一起。您可以根據需要選擇合適的方法進行使用。確保你的項目中引入了正確的Hutool依賴,并驗證這些方法在你的項目環境中可用。

StrUtil

以下是Hutool庫中StrUtil類的常用方法整理在一起:

import cn.hutool.core.util.StrUtil;public class StrUtilExample {public static void main(String[] args) {// 判斷字符串是否為空boolean isEmpty = StrUtil.isEmpty("hello");// 判斷字符串是否為空白(包括空格、制表符、換行符等)boolean isBlank = StrUtil.isBlank("  ");// 判斷字符串是否不為空boolean isNotEmpty = StrUtil.isNotEmpty("world");// 判斷字符串是否不為空白boolean isNotBlank = StrUtil.isNotBlank("Hello, world!");// 去除字符串兩端的空白字符String trimmedStr = StrUtil.trim("  Hutool  ");// 字符串拼接String concatStr = StrUtil.join(",", "a", "b", "c");// 字符串格式化String formattedStr = StrUtil.format("Hello, {}!", "world");// 字符串分割List<String> splitList = StrUtil.split("a,b,c", ",");// 字符串替換String replacedStr = StrUtil.replace("Hello, world!", "world", "Hutool");// 字符串轉換為大寫String upperCaseStr = StrUtil.upperFirst("hutool");// 字符串轉換為小寫String lowerCaseStr = StrUtil.lowerFirst("HUTOOL");// 截取字符串String subStr = StrUtil.sub("Hutool is a useful tool", 6, 13);}
}

BeanUtil

以下是Hutool庫中BeanUtil類的常用方法整理在一起:?

import cn.hutool.core.bean.BeanUtil;public class BeanUtilExample {public static void main(String[] args) {// 復制屬性值到新對象MyBean sourceBean = new MyBean("John", 30);MyBean targetBean = new MyBean();BeanUtil.copyProperties(sourceBean, targetBean);// 將Map轉換為Bean對象Map<String, Object> map = new HashMap<>();map.put("name", "Alice");map.put("age", 25);MyBean beanFromMap = BeanUtil.mapToBean(map, MyBean.class, false);// 將Bean對象轉換為MapMap<String, Object> beanMap = BeanUtil.beanToMap(targetBean);// 判斷Bean對象是否為空對象boolean isBeanEmpty = BeanUtil.isEmpty(targetBean);// 獲取Bean對象的指定屬性值Object nameValue = BeanUtil.getProperty(targetBean, "name");// 設置Bean對象的指定屬性值BeanUtil.setProperty(targetBean, "age", 35);}static class MyBean {private String name;private int age;// Constructor, getters, and setters}
}

CollUtil?

?以下是Hutool庫中CollUtil類的常用方法整理在一起:

import cn.hutool.core.collection.CollUtil;public class CollUtilExample {public static void main(String[] args) {// 創建ArrayList集合List<String> list = CollUtil.newArrayList("apple", "banana", "cherry");// 判斷集合是否為空boolean isEmpty = CollUtil.isEmpty(list);// 判斷集合是否不為空boolean isNotEmpty = CollUtil.isNotEmpty(list);// 集合元素拼接成字符串String joinStr = CollUtil.join(list, ",");// 將集合轉換為數組String[] array = CollUtil.toArray(list, String.class);// 判斷集合中是否包含指定元素boolean contains = CollUtil.contains(list, "banana");// 獲取集合的指定下標元素String element = CollUtil.get(list, 1);// 反轉集合順序List<String> reversedList = CollUtil.reverse(list);// 清空集合CollUtil.clear(list);}
}

FileUtil?

在 Hutool 中,FileUtil 類提供了一系列用于文件操作的工具方法。以下是 FileUtil 的常見用法示例:

文件讀寫操作

import cn.hutool.core.io.FileUtil;public class FileUtilExample {public static void main(String[] args) {// 讀取文件內容為字符串String content = FileUtil.readUtf8String("test.txt");// 將字符串寫入文件FileUtil.writeUtf8String("Hello, World!", "output.txt");// 復制文件FileUtil.copy("source.txt", "destination.txt", true);// 移動文件FileUtil.move("source.txt", "destination.txt", true);// 刪除文件FileUtil.del("fileToDelete.txt");// 創建目錄FileUtil.mkdir("newDirectory");// 獲取文件擴展名String extName = FileUtil.extName("example.doc");// 獲取文件大小long fileSize = FileUtil.size("largeFile.pdf");}
}

遍歷文件和目錄

import cn.hutool.core.io.FileUtil;
import java.io.File;
import java.util.List;public class FileTraverseExample {public static void main(String[] args) {// 遍歷指定目錄下的所有文件,包括子目錄List<File> fileList = FileUtil.loopFiles("directoryPath");// 遍歷指定目錄下的所有子目錄List<File> dirList = FileUtil.loopFiles("directoryPath", file -> file.isDirectory());}
}

以上示例展示了如何使用 Hutool 中的 FileUtil 類進行文件操作,包括讀取文件內容、寫入文件、復制、移動、刪除文件,創建目錄,獲取文件擴展名和大小,以及遍歷文件和目錄。確保你的項目中引入了正確的 Hutool 依賴,并驗證這些方法在你的項目環境中可用。

IOUtil

在 Hutool 中,IOUtil 類提供了一些實用的方法來處理輸入輸出流。以下是 IOUtil 的一些常見用法示例:

輸入流操作

import cn.hutool.core.io.IoUtil;public class IoUtilExample {public static void main(String[] args) {// 從輸入流中讀取字節數組byte[] bytes = IoUtil.readBytes(inputStream);// 從輸入流中讀取字符串String content = IoUtil.readUtf8(inputStream);// 將輸入流內容復制到輸出流long copySize = IoUtil.copy(inputStream, outputStream, bufferSize);// 關閉輸入流IoUtil.close(inputStream);}
}

輸出流操作

import cn.hutool.core.io.IoUtil;public class IoUtilExample {public static void main(String[] args) {// 從輸入流中讀取字節數組byte[] bytes = IoUtil.readBytes(inputStream);// 從輸入流中讀取字符串String content = IoUtil.readUtf8(inputStream);// 將輸入流內容復制到輸出流long copySize = IoUtil.copy(inputStream, outputStream, bufferSize);// 關閉輸入流IoUtil.close(inputStream);}
}

文件流操作

import cn.hutool.core.io.IoUtil;public class IoUtilExample {public static void main(String[] args) {// 從文件中讀取字節數組byte[] fileBytes = IoUtil.readBytes(file);// 從文件中讀取字符串String fileContent = IoUtil.readUtf8(file);// 將字節數組寫入文件IoUtil.write(fileBytes, file);// 將字符串寫入文件IoUtil.writeUtf8("Hello, World!", file);}
}

以上示例展示了如何使用 Hutool 中的 IOUtil 類來處理輸入輸出流。確保你的項目中引入了正確的 Hutool 依賴,并驗證這些方法在你的項目環境中可用。

ObjectUtil

在 Hutool 中,ObjectUtil 類提供了一些實用的方法來操作對象。以下是 ObjectUtil 的一些常見用法示例:

對象判空和相等性檢查

import cn.hutool.core.util.ObjectUtil;public class ObjectUtilExample {public static void main(String[] args) {Object obj1 = null;Object obj2 = new Object();// 判斷對象是否為空boolean isNull = ObjectUtil.isNull(obj1);// 判斷對象是否非空boolean isNotNull = ObjectUtil.isNotNull(obj2);// 比較兩個對象是否相等(支持處理null情況)boolean isEqual = ObjectUtil.equal(obj1, obj2);}
}

克隆對象

import cn.hutool.core.util.ObjectUtil;public class ObjectCloneExample {public static void main(String[] args) {SomeClass originalObj = new SomeClass();// 淺克隆對象SomeClass clonedObj = ObjectUtil.clone(originalObj);// 深克隆對象(需要對象實現 Serializable 接口)SomeClass deepClonedObj = ObjectUtil.deepClone(originalObj);}
}

Convert?

在 Hutool 中,Convert 類提供了一系列用于類型轉換的方法。以下是 Convert 類的一些常見用法示例:

類型轉換

import cn.hutool.core.convert.Convert;public class ConvertExample {public static void main(String[] args) {// 將字符串轉換為整數int intValue = Convert.toInt("123");// 將字符串數組轉換為整數數組int[] intArray = Convert.toIntArray(new String[]{"1", "2", "3"});// 將對象轉換為字符串String strValue = Convert.toStr(123);// 將對象轉換為日期Date dateValue = Convert.toDate("2024-02-21");// 將對象轉換為指定類型double doubleValue = Convert.convert(double.class, "3.14");// 將集合轉換為數組List<String> list = CollUtil.newArrayList("apple", "banana", "cherry");String[] array = Convert.convert(String[].class, list);}
}

轉換為指定類型

import cn.hutool.core.convert.Convert;public class ConvertToTypeExample {public static void main(String[] args) {// 將字符串轉換為枚舉類型Status status = Convert.toEnum(Status.class, "ACTIVE");// 將對象轉換為指定實體類User user = Convert.convert(User.class, dataMap);}
}

以上示例展示了如何使用 Hutool 中的 Convert 類進行類型轉換操作。確保你的項目中引入了正確的 Hutool 依賴,并驗證這些方法在你的項目環境中可用。

NumberUtil

在 Hutool 中,NumberUtil 類提供了一些實用的方法來處理數字類型。以下是 NumberUtil 的一些常見用法示例:

數字格式化

import cn.hutool.core.util.NumberUtil;public class NumberUtilExample {public static void main(String[] args) {// 格式化保留小數位數double num = 123.456789;String formattedNumber = NumberUtil.decimalFormat("#.##", num);// 四舍五入保留指定小數位double roundedNumber = NumberUtil.round(num, 2);// 獲取兩數相加的和int sum = NumberUtil.add(10, 20);// 獲取兩數相減的差int difference = NumberUtil.sub(30, 10);// 獲取兩數相乘的積int product = NumberUtil.mul(5, 6);// 獲取兩數相除的商double quotient = NumberUtil.div(10, 3);}
}

其他數值操作

import cn.hutool.core.util.NumberUtil;public class NumberUtilOperationsExample {public static void main(String[] args) {// 判斷一個數是否為整數boolean isInteger = NumberUtil.isInteger("123");// 判斷一個數是否為質數boolean isPrime = NumberUtil.isPrimes(7);// 獲取最大公約數long gcd = NumberUtil.gcd(24, 36);// 獲取最小公倍數long lcm = NumberUtil.lcm(4, 6);}
}

ZipUtil

在 Hutool 中,ZipUtil 類提供了用于壓縮和解壓縮文件或目錄的方法。以下是 ZipUtil 的一些常見用法示例:

壓縮文件或目錄

import cn.hutool.core.util.ZipUtil;public class ZipUtilExample {public static void main(String[] args) {// 壓縮單個文件ZipUtil.zip("sourceFile.txt", "compressedFile.zip");// 壓縮目錄及其子目錄ZipUtil.zip("sourceDirectory", "compressedDirectory.zip");}
}

解壓縮文件

import cn.hutool.core.util.ZipUtil;public class UnzipUtilExample {public static void main(String[] args) {// 解壓縮文件到指定目錄ZipUtil.unzip("compressedFile.zip", "unzippedDirectory");}
}

壓縮和解壓縮操作示例

import cn.hutool.core.util.ZipUtil;
import java.io.File;public class ZipAndUnzipExample {public static void main(String[] args) {// 壓縮目錄ZipUtil.zip(new File("sourceDirectory"), new File("compressedDirectory.zip"));// 解壓縮文件ZipUtil.unzip(new File("compressedDirectory.zip"), new File("unzippedDirectory"));}
}

以上示例展示了如何使用 Hutool 中的 ZipUtil 類來進行文件或目錄的壓縮和解壓縮操作。確保你的項目中引入了正確的 Hutool 依賴,并驗證這些方法在你的項目環墯中可用。

官網地址

簡介 | Hutool

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/696524.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/696524.shtml
英文地址,請注明出處:http://en.pswp.cn/news/696524.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【鴻蒙 HarmonyOS 4.0】數據持久化

一、數據持久化介紹 數據持久化是將內存數據(內存是臨時的存儲空間)&#xff0c;通過文件或數據庫的形式保存在設備中。 HarmonyOS提供兩種數據持久化方案&#xff1a; 1.1、用戶首選項&#xff08;Preferences&#xff09;&#xff1a; 通常用于保存應用的配置信息。數據通…

android 全局異常處理封裝

app出現了問題&#xff0c;尤其是多線程問題&#xff0c;某個線程出了問題&#xff0c;很不好找&#xff0c;那是不是可以搞一個統一的處理類&#xff0c;將所有的異常信息都統一到一個地方呢&#xff0c;原本只是一個知識點&#xff0c;但我發現這里還可以 保存異常信息到本地…

Vue 進階系列丨實現簡易reactive和ref

Vue 進階系列教程將在本號持續發布&#xff0c;一起查漏補缺學個痛快&#xff01;若您有遇到其它相關問題&#xff0c;非常歡迎在評論中留言討論&#xff0c;達到幫助更多人的目的。若感本文對您有所幫助請點個贊吧&#xff01; 2013年7月28日&#xff0c;尤雨溪第一次在 GItHu…

計算機網絡Day02--物理層(一)

計算機網絡Day02–物理層 物理層基本概念 物理層考慮的是怎么才能在連接各種計算機的傳輸媒體上傳輸比特流&#xff0c;而不是具體的傳輸媒體 作用&#xff1a;盡可能屏蔽掉不同傳輸媒體和通信手段的差異 用于物流層的協議也稱為物流層規程 主要作用&#xff1a;解決計算機…

COCI2015-2016#1 RELATIVNOST

P6533 [COCI2015-2016#1] RELATIVNOST 題目大意 小 L L L在賣畫。這些畫分為彩色畫和黑白畫&#xff0c;小 L L L希望有至少 c c c個人會買走他至少一張彩色畫。 第 i i i個人至多會購買 a i a_i ai?張彩色畫或者 b i b_i bi?張黑白畫&#xff0c;且每個人至少購買一張畫。…

Android---Jetpack Compose學習007

Compose 附帶效應 a. 純函數 純函數指的是函數與外界交換數據只能通過函數參數和函數返回值來進行&#xff0c;純函數的運行不會對外界環境產生任何的影響。比如下面這個函數&#xff1a; fun Add(a : Int, b : Int) : Int {return a b } “副作用”&#xff08;side effe…

單例模式的介紹

單例模式&#xff08;Singleton&#xff09;是一種創建型設計模式&#xff0c;它確保一個類只有一個實例&#xff0c;并提供全局訪問點。其核心思想是通過限制類的實例化次數&#xff0c;防止多個實例同時存在&#xff0c;從而避免了多線程競爭和資源浪費&#xff0c;提高了代碼…

【藍橋杯單片機入門記錄】靜態數碼管

目錄 一、數碼管概述 &#xff08;1&#xff09;認識數碼管 &#xff08;2&#xff09;數碼管的工作原理 &#xff08;3&#xff09;LED數碼管驅動方式-靜態顯示 二、數碼管電路圖 三、靜態數碼管顯示例程 &#xff08;1&#xff09;例程1&#xff1a;數碼管顯示某一位&a…

vue、thinkphp實現騰訊云對象存儲COS圖片上傳

環境&#xff1a; thinkphp6 vue2 vant2.12 composer安裝qcloud-sts-sdk composer require qcloud_sts/qcloud-sts-sdk獲取COS臨時id、key的sts接口 <?php declare (strict_types 1);namespace app\index\controller; use QCloud\COSSTS\Sts;class CosController {//h…

如何為PostgreSQL設置自增主鍵?

在 PostgreSQL 中&#xff0c;自增主鍵通常是通過使用 SERIAL 類型或在新版本中使用 IDENTITY 列來實現的。 1. 使用 SERIAL 類型 SERIAL 是一個自動增加的整數&#xff0c;常用于主鍵。當插入新的行時&#xff0c;PostgreSQL 會自動為這個列生成一個新的值。 ??例如 CREAT…

PYQT5-自定義事件

from PyQt5.QtCore import QEvent, QObject from PyQt5.QtWidgets import QApplication import sys# 自定義事件類 class CustomEvent(QEvent):# PYQT5 預留給用戶自定義事件類型的起點為 QEvent.User1000custom_event_type QEvent.registerEventType()# 也可以這樣寫# custom…

2024.2.22

P1162 #include<map> #include<vector> #include<iostream> #include<math.h> #include<algorithm> #include<string> using namespace std; const int N 1020; int n; int g[N][N];//標記數組 int a[N][N];//儲存數組 int dx[] { -1…

webstorm光標變成方塊解決辦法_webstorm光標變粗不能換行

webstorms光標變了 鍵盤上的insert是切換的快捷鍵&#xff0c;敲insert就可以來回切換了

回顧 | Java面向對象 多態篇

多態是面向對象編程中的一個重要概念&#xff0c;它允許不同的對象對同一消息做出不同的響應。 通過多態&#xff0c;可以通過父類或接口定義的引用變量來操作子類或實現類的對象&#xff0c;從而實現同一方法在不同對象上的不同行為。 在Java中&#xff0c;多態性主要通過繼…

雙通道并行網絡,想用哪個網絡用哪個,MATLAB代碼

本期可謂是寶藏篇&#xff01;學會本期的思想&#xff0c;幫助你分分鐘找到創新點&#xff0c;且不與別人重復&#xff01; 本期采用MATLAB代碼&#xff0c;實現一種“基于格拉姆角場與并行CNN的故障診斷方法”。該方法的具體實現可以參考文獻&#xff1a; [1]李宗源,陳謙,錢…

React native更改包名后,啟動app的activity包名不生效問題

這篇文章本不算記錄的&#xff0c;因為實際開發中&#xff0c;類似這種小問題會有很多很多&#xff0c;因為導致問題的原因千奇百怪&#xff0c;解決方案也不盡相同&#xff0c;所以也都沒有記錄。 但今天看到我10年寫的問題解決小文章&#xff0c;被網友收藏了&#xff0c; 感…

普中51單片機學習(EEPROM)

EEPROM IIC串行總線的組成及工作原理 I2C總線的數據傳送 數據位的有效性規定 I2C總線進行數據傳送時&#xff0c;時鐘信號為高電平期間&#xff0c;數據線上的數據必須保持穩定&#xff0c;只有在時鐘線上的信號為低電平期間&#xff0c;數據線上的高電平或低電平狀態才允許…

分享WebGL物體三維建模

界面效果 代碼結構 模型素材類似CT (Computed Tomography)&#xff0c;即電子計算機斷層掃描&#xff0c;它是利用精確準直的X線束、γ射線、超聲波等&#xff0c;與靈敏度極高的探測器一同圍繞物體的某一部位作一個接一個的斷面掃描。 坐標系統 渲染流程 渲染流程是個將之前準…

Sora:OpenAI引領AI視頻新時代

Sora - 探索AI視頻模型的無限可能 隨著人工智能技術的飛速發展&#xff0c;AI視頻模型已成為科技領域的新熱點。而在這個浪潮中&#xff0c;OpenAI推出的首個AI視頻模型Sora&#xff0c;以其卓越的性能和前瞻性的技術&#xff0c;引領著AI視頻領域的創新發展。讓我們將一起探討…

C++(12) 模板類、模板繼承(嚴格模式和自由模式)

文章目錄 模版類1. 模版類2. 模版參數限制3. 模版繼承3.1 嚴格模式3.2 自由模式 4. 模版類的模版函數5. 返回值類型帶有模版 模版類 1. 模版類 #include <iostream>using namespace std;/* 當前 Person 類型&#xff0c;聲明了連個模版分別對應NameType 模版類型&#…