❤️ Author home page: Xiaoxuzhu
❤️ About the author: Hello, I'm Xiao xuzhu. High quality creators in Java field 🏆, CSDN blog expert 🏆, Huawei cloud sharing expert 🏆, Popular author of the year 🏆, Alibaba cloud expert Blogger 🏆
❤️ Technical work should be rewarded
❤️ give the thumbs-up 👍 Collection ⭐ Look again and form a habit
Purpose: ResourceUtil resource tool
Usage scenario
Resource tool class: it can be used to read the resources under Classpath and obtain the resource list under the specified path;
Method summary
method | describe |
---|---|
cn.hutool.core.io.resource.ResourceUtil.readUtf8Str(java.lang.String) |
Read the resource under Classpath as a string and use UTF-8 encoding
|
cn.hutool.core.io.resource.ResourceUtil.readStr(java.lang.String, java.nio.charset.Charset) |
Read the resource under Classpath as a string
|
cn.hutool.core.io.resource.ResourceUtil.readBytes(java.lang.String) |
Read the resource under Classpath as byte []
|
cn.hutool.core.io.resource.ResourceUtil.getStream(java.lang.String) |
Get {@ link InputStream} from ClassPath resource
|
cn.hutool.core.io.resource.ResourceUtil.getStreamSafe(java.lang.String) |
Get {@ link InputStream} from the ClassPath resource, and return null when the resource does not exist
|
cn.hutool.core.io.resource.ResourceUtil.getUtf8Reader(java.lang.String) |
Get {@ link BufferedReader} from ClassPath resource
|
cn.hutool.core.io.resource.ResourceUtil.getReader(java.lang.String, java.nio.charset.Charset) |
Get {@ link BufferedReader} from ClassPath resource
|
cn.hutool.core.io.resource.ResourceUtil.getResource(java.lang.String) |
The URL < br > path to obtain resources is separated by /. For example:
config/a/db.config spring/xml/test.xml |
cn.hutool.core.io.resource.ResourceUtil.getResources(java.lang.String) |
Get the resource list under the specified path < br > the path format must be directory format, separated by /. For example:
config/a spring/xml |
cn.hutool.core.io.resource.ResourceUtil.getResourceIter(java.lang.String) |
Get the resources under the specified path. Iterator < br > the path format must be directory format, separated by /. For example:
config/a spring/xml |
cn.hutool.core.io.resource.ResourceUtil.getResource(java.lang.String, java.lang.Class) |
Obtain the URL corresponding to the relative path of the resource
|
cn.hutool.core.io.resource.ResourceUtil.getResourceObj(java.lang.String) |
Get the {@ link Resource} resource object < br > if the provided path is an absolute path or the path starts with file:, return {@ link FileResource}, otherwise return {@ link ClassPathResource}
|
Method details
Method name: CN hutool. core. io. resource. ResourceUtil. readUtf8Str(java.lang.String)
Method description
Read the resource under Classpath as a string and use UTF-8 encoding
Supported version and above
3.1.1
Parameter Description:
Parameter name | describe |
---|---|
String resource |
Resource resource path, using the path relative to ClassPath
|
Return value:
Resource content
Reference case:
final String str = ResourceUtil.readUtf8Str("test.xml"); System.out.println(str); Assert.assertNotNull(str); System.out.println("------------------");
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. resource. ResourceUtil. readStr(java.lang.String, java.nio.charset.Charset)
Method description
Read the resource under Classpath as a string
Supported version and above
3.1.1
Parameter Description:
Parameter name | describe |
---|---|
String resource |
resource can be an absolute path or a relative path (relative ClassPath)
|
Charset charset |
charset coding
|
Return value:
Resource content
Reference case:
final String str = ResourceUtil.readStr("test.xml", CharsetUtil.CHARSET_UTF_8); System.out.println(str); Assert.assertNotNull(str); System.out.println("------------------"); final String str1 = ResourceUtil.readStr("C:\\Users\\Administrator\\Desktop\\xuzhu\\aaa.txt", CharsetUtil.CHARSET_UTF_8); System.out.println(str1);
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. resource. ResourceUtil. readBytes(java.lang.String)
Method description
Read the resource under Classpath as byte []
Supported version and above
4.5.19
Parameter Description:
Parameter name | describe |
---|---|
String resource |
resource can be an absolute path or a relative path (relative ClassPath)
|
Return value:
Resource content
Reference case:
final byte[] bytes = ResourceUtil.readBytes("test.xml"); System.out.println(new String(bytes,CharsetUtil.CHARSET_UTF_8));
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. resource. ResourceUtil. getStream(java.lang.String)
Method description
Get {@ link InputStream} from ClassPath resource
Supported version and above
3.1.2
Parameter Description:
Parameter name | describe |
---|---|
String resource |
resource ClassPath resource
|
Return value:
{@link InputStream}
Reference case:
final byte[] bytes = IoUtil.readBytes(ResourceUtil.getStream("hutool.jpg")); Assert.assertEquals(22807, bytes.length);
Source code analysis:
/** * Get {@ link InputStream} from ClassPath resource * * @param resource ClassPath resources * @return {@link InputStream} * @throws NoResourceException There is no exception in the resource * @since 3.1.2 */ public static InputStream getStream(String resource) throws NoResourceException { return getResourceObj(resource).getStream(); }
Method details
Method name: CN hutool. core. io. resource. ResourceUtil. getStreamSafe(java.lang.String)
Method description
Get {@ link InputStream} from the ClassPath resource, and return null when the resource does not exist
Supported version and above
4.0.3
Parameter Description:
Parameter name | describe |
---|---|
String resource |
resource ClassPath resource
|
Return value:
{@link InputStream}
Reference case:
//The effect is the same as that of readBytes, except that try catch is added, and Null is returned in case of exception final byte[] bytes = IoUtil.readBytes(ResourceUtil.getStreamSafe("hutool.jpg")); Assert.assertEquals(22807, bytes.length);
Source code analysis:
/** * Get {@ link InputStream} from the ClassPath resource, and return null when the resource does not exist * * @param resource ClassPath resources * @return {@link InputStream} * @since 4.0.3 */ public static InputStream getStreamSafe(String resource) { try { return getResourceObj(resource).getStream(); } catch (NoResourceException e) { // ignore } return null; }
Method details
Method name: CN hutool. core. io. resource. ResourceUtil. getUtf8Reader(java.lang.String)
Method description
Get {@ link BufferedReader} from ClassPath resource
Supported version and above
5.3.6
Parameter Description:
Parameter name | describe |
---|---|
String resource |
resource ClassPath resource
|
Return value:
{@link InputStream}
Reference case:
final CsvReader reader = CsvUtil.getReader(); final List<Map<String, String>> result = reader.readMapList( ResourceUtil.getUtf8Reader("test_bean.csv")); Assert.assertEquals("Zhang San", result.get(0).get("full name")); Assert.assertEquals("male", result.get(0).get("gender")); Assert.assertEquals("nothing", result.get(0).get("focus")); Assert.assertEquals("33", result.get(0).get("age")); Assert.assertEquals("Li Si", result.get(1).get("full name")); Assert.assertEquals("male", result.get(1).get("gender")); Assert.assertEquals("Good object", result.get(1).get("focus")); Assert.assertEquals("23", result.get(1).get("age")); Assert.assertEquals("Mei Mei Wang", result.get(2).get("full name")); Assert.assertEquals("female", result.get(2).get("gender")); Assert.assertEquals("Special attention", result.get(2).get("focus")); Assert.assertEquals("22", result.get(2).get("age"));
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. resource. ResourceUtil. getReader(java.lang.String, java.nio.charset.Charset)
Method description
Get {@ link BufferedReader} from ClassPath resource
Supported version and above
3.1.2
Parameter Description:
Parameter name | describe |
---|---|
String resource |
resource ClassPath resource
|
Charset charset |
charset coding
|
Return value:
{@link InputStream}
Reference case:
CsvReader reader = new CsvReader(); CsvData data = reader.read(ResourceUtil.getReader("test.csv", CharsetUtil.CHARSET_UTF_8)); Assert.assertEquals("sss,sss", data.getRow(0).get(0)); Assert.assertEquals("Gender", data.getRow(0).get(2)); Assert.assertEquals("follow\"object\"", data.getRow(0).get(3));
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. resource. ResourceUtil. getResource(java.lang.String)
Method description
Get the URL of the resource < br >
Paths are separated by / for example:
config/a/db.config spring/xml/test.xml
Supported version and above
Parameter Description:
Parameter name | describe |
---|---|
String resource |
Resource resource (path relative to Classpath)
|
Return value:
Resource URL
Reference case:
URL url = ResourceUtil.getResource("hutool.jpg"); System.out.println(url);
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. resource. ResourceUtil. getResources(java.lang.String)
Method description
Get the resource list under the specified path < br >
The path format must be directory format, separated by /. For example:
config/a spring/xml
Supported version and above
Parameter Description:
Parameter name | describe |
---|---|
String resource |
Resource resource path
|
Return value:
resource list
Reference case:
List<URL> urls = ResourceUtil.getResources("test.xml"); for (URL url : urls) { System.out.println(url); }
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. resource. ResourceUtil. getResourceIter(java.lang.String)
Method description
Get the resource iterator under the specified path < br >
The path format must be directory format, separated by /. For example:
config/a spring/xml
Supported version and above
4.1.5
Parameter Description:
Parameter name | describe |
---|---|
String resource |
Resource resource path
|
Return value:
resource list
Reference case:
EnumerationIter<URL> urls = ResourceUtil.getResourceIter("test.xml"); for (URL url : urls) { System.out.println(url); }
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. resource. ResourceUtil. getResource(java.lang.String, java.lang.Class)
Method description
Obtain the URL corresponding to the relative path of the resource
Supported version and above
Parameter Description:
Parameter name | describe |
---|---|
String resource |
Resource resource relative path
|
java.lang.Class baseClass |
baseClass is the benchmark Class. The relative path obtained is relative to the path of this Class. If it is {@ code null}, it is relative to ClassPath
|
Return value:
{@link URL}
Reference case:
URL url = ResourceUtil.getResource("FileUtil.class",FileUtil.class); System.out.println(url); System.out.println("----------------"); url = ResourceUtil.getResource("hutool.jpg",null); System.out.println(url);
Source code analysis:
Link: to be added
Method details
Method name: CN hutool. core. io. resource. ResourceUtil. getResourceObj(java.lang.String)
Method description
Get {@ link Resource} resource object < br >
If the provided path is absolute or starts with file:, return {@ link FileResource}; otherwise return {@ link ClassPathResource}
Supported version and above
3.2.1
Parameter Description:
Parameter name | describe |
---|---|
String path |
Path path can be absolute path or relative path (relative ClassPath)
|
Return value:
{@ link Resource} resource object
Reference case:
Resource resource = ResourceUtil.getResourceObj("test.xml"); System.out.println(resource.readUtf8Str());
Source code analysis:
Link: to be added
Today is the 27th / 100th day of continuous writing.
You can follow me, praise me, comment on me and collect me.