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

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.
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Flash Player will no longer be supported after December 2020. Turn off [Google Chrome] - Chrome
- [fix] White Screen when open Teams on Windows - Teams
- How to create new user account in Windows bash - Bash
- Add or remove users from sudo group - Ubuntu - Ubuntu
- Send Email with attachment using SharePoint PowerShell, SMTP server - SharePoint
- Fix: Teams is in preview in Safari on Mac - Teams
- Change Line Endings (Encoding Windows/Mac/Unix CR/LF/CRLF) Sublime Text - Sublime-Text
- 100+ SEO Tips to improve your website search ranking - HowTos