- Properties File — Java Read & Write
- Java properties file reader example
- How to read a properties file line by line in java
- How to write a key and values to a properties file in java
- Conclusion
- How to read the data from a properties file in Java?
- Syntax
- credentials.properties file
- Example
- Output
- How to read properties file in java
- Read properties file from System
- Read properties file from classpath
- Was this post helpful?
- Related posts
- Count Files in Directory in Java
- How to Remove Extension from Filename in Java
- How to Get Temp Directory Path in Java
- Convert Outputstream to Byte Array in Java
- How to get current working directory in java
- Difference between Scanner and BufferReader in java
- Read UTF-8 Encoded Data in java
- Write UTF-8 Encoded Data in java
- Java read file line by line
- Java FileWriter Example
- Java FileReader Example
- Java – Create new file
- Share this
- Related Posts
- Author
- Related Posts
- Count Files in Directory in Java
- How to Remove Extension from Filename in Java
- How to Get Temp Directory Path in Java
- Convert Outputstream to Byte Array in Java
- How to get current working directory in java
- Difference between Scanner and BufferReader in java
Properties File — Java Read & Write
This tutorial explains step by step to read and write a properties file in java with example You learned to read properties file with key and values as well as line by line and also write key and values to the properties file.
In this tutorial, How to read and write a properties file content in Java
Java properties file reader example
In this example, you will learn how to read a key and its values from a properties file and display it to console
Let’s declare the properties file
Create a properties object, Properties is a data structure to store keys and values in java
Create URL object using ClassLoader . getSystemResource method
Create a InputStream using url.openStream method
Load the properties content into the java properties object using the load method
Add try and catch block for IOExceptions and FIleNotFoundException
Oneway, Print the value using getProperty with the key of a properties object
Another way is to iterate properties object for the loop
First, get all keys using stringPropertyNames
using for loop print the key and values.
catch (FileNotFoundException fie) < fie.printStackTrace(); >catch (IOException e) < e.printStackTrace(); >System.out.println(properties.getProperty("hostname")); Set keys = properties.stringPropertyNames(); for (String key : keys) < System.out.println(key + " - " + properties.getProperty(key)); >> >
How to read a properties file line by line in java
- created a File object with an absolute path
- Create BufferedReader using FileReader object
- get the First line of the properties file using readLine of BufferedReader
- Loop using while loop until the end of the line reached
- Print each line
> catch (FileNotFoundException e1) < e1.printStackTrace(); >catch (IOException e) < e.printStackTrace(); >finally < try < br.close(); >catch (IOException e) < e.printStackTrace(); >> > >
How to write a key and values to a properties file in java
In this example, You can read and write a property using
- First create a File object
- Create a writer object using FileWriter
- Create properties object and add new properties or update existing properties if the properties file exists
- setProperties method do update or add key and values
- store method of properties object writes to the properties file, You have to add the comment which appends to the properties file as a comment
Here is a complete example read and write a properties file
catch (FileNotFoundException e) < e.printStackTrace(); >catch (IOException e) < e.printStackTrace(); >> >
Conclusion
You learned to read a properties file with keys and values as well as line by line and also write keys and values to the properties file
How to read the data from a properties file in Java?
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
How to read properties file in java
In this post , we will see how to read properties file in java.
Properties files are used in java projects to externalise configuration, for example, database settings.
Java uses Properties class to store the above key-values pair. Properties.load() method is very convenient to load properties file in form of key-values pairs.
Properties file looks something like this.
There are two ways you can do it.
Read properties file from System
In this, you need to read properties file from system path. Here I am putting properties file in root level of project.
When you run above program, you will get following output:
Reading from properties file
—————————–
host : localhost
username : java2blog
password : java123
—————————–
Read properties file from classpath
You can read properties file to classpath too. You have $project/src as default classpath as this src folder will be copied to classes. You can put it in $project/src folder and read it from there.
you need to use this.getClass().getResourceAsStream(«/config.properties»); to read it from classpath .
When you run above program, you will get following output:
Reading from properties file
—————————–
host : localhost
username : java2blog
password : java123
—————————–
That’s all about how to read properties file in java
Was this post helpful?
Related posts
Count Files in Directory in Java
How to Remove Extension from Filename in Java
How to Get Temp Directory Path in Java
Convert Outputstream to Byte Array in Java
How to get current working directory in java
Difference between Scanner and BufferReader in java
Read UTF-8 Encoded Data in java
Write UTF-8 Encoded Data in java
Java read file line by line
Java FileWriter Example
Java FileReader Example
Java – Create new file
Share this
Related Posts
Author
Related Posts
Count Files in Directory in Java
Table of ContentsUsing java.io.File ClassUse File.listFiles() MethodCount Files in the Current Directory (Excluding Sub-directories)Count Files in the Current Directory (Including Sub-directories)Count Files & Folders in Current Directory (Excluding Sub-directories)Count Files & Folders in Current Directory (Including Sub-directories)Use File.list() MethodUsing java.nio.file.DirectoryStream ClassCount Files in the Current Directory (Excluding Sub-directories)Count Files in the Current Directory (Including Sub-directories)Count […]
How to Remove Extension from Filename in Java
Table of ContentsWays to Remove extension from filename in javaUsing substring() and lastIndexOf() methodsUsing replaceAll() methodUsing Apache common library In this post, we will see how to remove extension from filename in java. Ways to Remove extension from filename in java There are multiple ways to remove extension from filename in java. Let’s go through […]
How to Get Temp Directory Path in Java
Table of ContentsGet Temp Directory Path in JavaUsing System.getProperty()By Creating Temp File and Extracting Temp PathUsing java.io.FileUsing java.nio.File.FilesOverride Default Temp Directory Path In this post, we will see how to get temp directory path in java. Get Temp Directory Path in Java Using System.getProperty() To get the temp directory path, you can simply use System.getProperty(«java.io.tmpdir»). […]
Convert Outputstream to Byte Array in Java
Table of ContentsConvert OutputStream to Byte array in JavaConvert OutputStream to ByteBuffer in Java In this post, we will see how to convert OutputStream to Byte array in Java. Convert OutputStream to Byte array in Java Here are steps to convert OutputStream to Byte array in java. Create instance of ByteArrayOutputStream baos Write data to […]
How to get current working directory in java
Difference between Scanner and BufferReader in java
Table of ContentsIntroductionScannerBufferedReaderDifference between Scanner and BufferedReader In this post, we will see difference between Scanner and BufferReader in java. Java has two classes that have been used for reading files for a very long time. These two classes are Scanner and BufferedReader. In this post, we are going to find major differences and similarities […]