- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to read the data from a properties file in Java?n
The Properties is a subclass of Hashtable class and it represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties.load() method of Properties class is convenient to load .properties file in the form of key-value pairs.
Syntax
public class Properties extends Hashtable
credentials.properties file
Example
import java.io.*; import java.util.*; public class ReadPropertiesFileTest { public static void main(String args[]) throws IOException { Properties prop = readPropertiesFile("credentials.properties"); System.out.println("username: "+ prop.getProperty("username")); System.out.println("password: "+ prop.getProperty("password")); } public static Properties readPropertiesFile(String fileName) throws IOException { FileInputStream fis = null; Properties prop = null; try { fis = new FileInputStream(fileName); prop = new Properties(); prop.load(fis); } catch(FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch(IOException ioe) { ioe.printStackTrace(); } finally { fis.close(); } return prop; } }
Output
username: admin password: admin@123
- Related Articles
- How to read the data from a properties file in Java?\n
- How to read/write data from/to .properties file in Java?
- How to read the data from a CSV file in Java?\n
- How to read the data from a file in Java?
- How to read data from .csv file in Java?
- How to read data in from a file to String using java?
- How to read data from one file and print to another file in Java?
- How to read data from a file using FileInputStream?
- How to read data from PDF file and display on console in Java?
- How to read data from *.CSV file using JavaScript?
- How to read a 2d array from a file in java?
- How to read integers from a file using BufferedReader in Java?
- Read Data from a Text File using C++
- How to read certain number of elements from a file in Java?
- How to create a file, write data into it and read data from it on iOS?
- Write a C program to read a data from file and display

Advertisements