In order to read a file from the resources folder make use of the getResourceAsStream method from the ClassLoader class,

Java project resources folder
Example:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
/**
*
* Read file from the resources folder
* using Java code.
*
*/
public class Main {
public static void main(String[] args) throws IOException {
String fileName="resource.properties";
String line;
ClassLoader classLoader = Main.class.getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(fileName);
if(inputStream == null) {
throw new IOException(fileName + " file not found!");
}
InputStreamReader streamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(streamReader);
while ((line = bufferedReader.readLine()) != null) {
String[] element = line.split("\\=");
System.out.println("Key: " + element[0]+" , Value: " +element[1]);
}
}
}
Output:
Key: name , Value: code2care
Key: username , Value: admin
Key: password , Value: 123456
You will receive a IOException if the file is not found.
More Posts related to Java,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- Disable Chrome Notification bell from Mac OS X menu bar - Mac-OS-X
- connection.url property value in hibernate.cfg.xml for mysql - Java
- [fix] bash: gradlew: command not found - Gradle
- Create S3 bucket using AWS CLI Command mb - AWS
- Show/Hide Hidden Files and Directories on Mac Finder - MacOS
- Java Program: Random Number Generator - Java
- Enable Dark Mode in Google Search - Google
- Device not compatible error Android Google Play Store - Android