In java many times we need to load property files or files of any type in java application. There are many ways to do this.
Table of Content :
- Introduction
- Using getResourceAsStream or getResource
- Using ResourceUtils.getFile()
- Using Google Guava
- Questions/Articles related to How to Read file from resources folder in Java
- Summary
1). Using getResourceAsStream or getResource
package com.javacodestuffs.io.files.read;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class ReadFileUsingResourceAsStream {
public static void main(String[] args) throws IOException {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("abc.csv");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
byte[] content = outputStream.toByteArray();
System.out.println("the content of the files are: ");
System.out.println(content);
}
}
package com.javacodestuffs.io.files.read;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class ReadFileFromResource2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ReadFileFromResource2 object = new ReadFileFromResource2();
object.readFiles();
}
public void readFiles() throws IOException {
InputStream is = this.getClass().getClassLoader().getResourceAsStream("abc.csv");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
byte[] content = outputStream.toByteArray();
System.out.println("the content of the files are: ");
System.out.println(content);
}
}
output:
the content of the files are:
[B@e73f9ac
2). ResourceUtils.getFile()
If your application is using the spring framework you can directly take benefits of ResourceUtils class.
The ResourceUtils class has Utility methods for resolving resource locations to files in the file system. Consider using Spring's Resource abstraction in the core package for handling all kinds of file resources in a uniform manner.
ResourceLoader's getResource() method can resolve any location to a Resource object, which in turn allows one to obtain a java.io.File in the file system through its getFile() method.
Maven dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.22</version>
</dependency>
package com.javacodestuffs.io.files.read;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.springframework.util.ResourceUtils;
public class ReadFileFromResourceUsingSpring {
public static void main(String[] args) throws IOException {
File file = ResourceUtils.getFile("classpath:application.properties");
System.out.println("The File Found :" + file.exists());
String content = new String(Files.readAllBytes(file.toPath()));
System.out.println("the file contents are : ");
System.out.println(content);
}
}
output:
db.driver: com.mysql.jdbc.Driver
db.url: jdbc:mysql://localhost/test_db?useSSL=false
db.username: root
db.password: root
3). Using Google Guava
Google Guava library can be used to Read file from resources folder in Java.
Maven dependencies
<dependency>
<groupId>com.google.guava </groupId>
<artifactId>guava </artifactId>
<version>29.0-jre </version>
</dependency>
package com.javacodestuffs.io.files.read;
import java.io.IOException;
import java.nio.charset.Charset;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
public class ReadFileFromResourceUsingGuava {
public static void main(String[] args) throws IOException {
String fileContent = readFileResource("config.properties", Charsets.UTF_8);
System.out.println("the file contents are : ");
System.out.println(fileContent);
}
public static String readFileResource(final String fileName, Charset charset) throws IOException {
return Resources.toString(Resources.getResource(fileName), charset);
}
}
output
the file contents are :
db.driver: com.mysql.jdbc.Driver
db.url: jdbc:mysql://localhost/test_db?useSSL=false
db.username: root
db.password: root
Post/Questions related to How to Read the file from resources folder in Java
How do you load properties files from the Resources folder in Java?
What is the Java Resources folder?
How do I read a file from the Resources folder in spring boot?
How do I read a resource folder?
How to read a file from src folder in java?
In this article, we have seen How to Read the file from the resources folder in Java. You can find this code examples in GitHub Respository
0 Comments
Post a Comment