在 Java 中获取资源 URL 和内容
本教程将演示如何在 Java 中使用 getResource()
函数获取资源 URL 和读取资源文件。
在 Java 中使用 getResource()
函数获取资源 URL
我们将使用 Java 中的 getResource()
方法来获取三个文件的 URL:image.png、image1.png、resourcetext.txt。
我们将在 getResource()
函数的主体中以字符串形式传递资源 URL。然后该函数搜索给定的资源字符串并返回一个包含 URL 的对象。
语法:
getResource(String);
public resource = yourclassname.getResource("Resource URL");
示例代码:
/*//you will learn how to get image URL in the following program
//import statements
*/
import java.net.URL;
import java.lang.*;
public class getImageUrl {
public static void main(String[] args) throws Exception {
getImageUrl obj = new getImageUrl();
@SuppressWarnings("rawtypes")
Class resource = obj.getClass();
URL imageurl = resource.getResource("/image.png");
System.out.println("Resource URL one is = " + imageurl);
URL imageurl2 = resource.getResource("/image2.png");
System.out.println("Resource URL two is = " + imageurl2);
URL texturl = resource.getResource("/textresource.txt");
System.out.println("Resource URL of the text file is = " + texturl);
}
}
输出:
Resource URL one is = file:/C:/Users/NAME/Desktop/JAVA/get%20resource%20url%20java/bin/image.png
Resource URL two is = file:/C:/Users/NAME/Desktop/JAVA/get%20resource%20url%20java/bin/image2.png
Resource URL of the text file is = file:/C:/Users/NAME/Desktop/JAVA/get%20resource%20url%20java/bin/textresource.txt
如我们所见,我们将这三个文件存储在字符串 URL 中。然后我们使用 obj.getClass()
方法来获取接收图像 URL 的主类。
getResource()
函数是返回 URL 的函数。
在 Java 中使用 getResourceAsStream()
获取资源内容
Java 保留了一个名为 getResourceAsStream()
的方法来读取文件。该函数返回一个 InputStream
对象,其中包含该类的指定资源。
对于下面的示例,我们将使用 getResourceAsStream()
来读取此文件:/get resource URL java/src/readfile/GetResourceReadFile.java
。还有字符串 getresourcefromhere = "readfile/example.json";
是我们存储 JSON 文件的地方。
语法:
private InputStream getFileFromResourceAsStream(String getresourcefromhere) {
ClassLoader cL = getClass().getClassLoader();
InputStream inputStream = cL.getResourceAsStream(getresourcefromhere);
return inputStream;
}
如果你了解基本语法,请查看以下完整程序。
该程序适用于每个平台。你应该注意主类和文件目录管理。
//import necessary packages
package readfile;
import java.io.*;
import java.nio.charset.StandardCharsets;
//start function
public class GetResourceReadFile {
private static final String String = null;
public static void main(String[] args) throws IOException, Exception {
GetResourceReadFile app = new GetResourceReadFile();
//get resource file
String getresourcefromhere = "readfile/example.json";
//use inputStream to return object containing resource
InputStream getresourceandreadit = app.getFileFromResourceAsStream(getresourcefromhere);
printInputStream(getresourceandreadit);
}
private InputStream getFileFromResourceAsStream(String getresourcefromhere) {
//load class
ClassLoader cL = getClass().getClassLoader();
InputStream inputStream = cL.getResourceAsStream(getresourcefromhere);
return inputStream;
}
private static void printInputStream(InputStream r) {
try (InputStreamReader sR =
new InputStreamReader(r, StandardCharsets.UTF_8);
BufferedReader R = new BufferedReader(sR)) {
String GOT_IT = String;
//not necessary but will give you basic idea
if (GOT_IT == String) {
//you can print multiple files
while ((GOT_IT = R.readLine()) != null) {
//print file
System.out.println(GOT_IT);
}
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
输出:
{
"File Name": "Demonstration File",
"File Type": "JSON FILE",
"File Reader": "getResource",
"File creation date:": 2/18/2022,
"Platform": {
"Langauge Type": "Programming",
"Langugae Name": "JAVA",
"Platform": "Oracle",
"Importance": "Very High"
},
"Development Environment": [
{ "JDK": "JAVA", "LATEST": "17" }
]
}
整个程序与前面带有 URL 的示例相同。唯一的区别是 InputStream
和 ClassLoader cL = getClass().getClassLoader();
。
Sarwan Soomro is a freelance software engineer and an expert technical writer who loves writing and coding. He has 5 years of web development and 3 years of professional writing experience, and an MSs in computer science. In addition, he has numerous professional qualifications in the cloud, database, desktop, and online technologies. And has developed multi-technology programming guides for beginners and published many tech articles.
LinkedIn