banner



How To Create Config File In Java

.properties is a file extension for files mainly used in Java related technologies to store the configurable parameters of an application. They can also be used for storing strings for Internationalization and localization; these are known as Property Resource Bundles.

Each parameter is stored as a pair of strings, one storing the name of the parameter (called the key/map ), and the other storing the value.

Below is a sample Java program which demonstrate you how to retrieve/read config.properties values in Java. For update follow this tutorial.

Another must read: Read config.properties value using Spring "singleton" Scope in your Java Enterprise Application

We will create 3 files:

  1. CrunchifyReadConfigMain.java
  2. CrunchifyGetPropertyValues.java
  3. config.properties file

Main Class (CrunchifyReadConfigMain.java) which will call getPropValues() method from classCrunchifyGetPropertyValues.java.

Let's get started:

Step-1: Create config.properties file.

  1. Create Folder "resources" under Java Resources folder if your project doesn't have it.
  2. create config.properties file with below value.

How to read config.properties in Java - Crunchify Tips

/Java Resources/config.properties file content:

#Crunchify Properties

user=Crunchify

company1=Google

company2=eBay

company3=Yahoo

Step-2

Create fileCrunchifyReadConfigMain.java

package crunchify . com . tutorial ;

import java . io . IOException ;

/**

* @author Crunchify.com

*

*/

public class CrunchifyReadConfigMain {

public static void main ( String [ ] args ) throws IOException {

CrunchifyGetPropertyValues properties = new CrunchifyGetPropertyValues ( ) ;

properties . getPropValues ( ) ;

}

}

Step-3

Create fileCrunchifyGetPropertyValues.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

package crunchify . com . tutorial ;

import java . io . FileNotFoundException ;

import java . io . IOException ;

import java . io . InputStream ;

import java . util . Date ;

import java . util . Properties ;

/**

* @author Crunchify.com

*

*/

public class CrunchifyGetPropertyValues {

String result = "" ;

InputStream inputStream ;

public String getPropValues ( ) throws IOException {

try {

Properties prop = new Properties ( ) ;

String propFileName = "config.properties" ;

inputStream = getClass ( ) . getClassLoader ( ) . getResourceAsStream ( propFileName ) ;

if ( inputStream != null ) {

prop . load ( inputStream ) ;

} else {

throw new FileNotFoundException ( "property file '" + propFileName + "' not found in the classpath" ) ;

}

Date time = new Date ( System . currentTimeMillis ( ) ) ;

// get the property value and print it out

String user = prop . getProperty ( "user" ) ;

String company1 = prop . getProperty ( "company1" ) ;

String company2 = prop . getProperty ( "company2" ) ;

String company3 = prop . getProperty ( "company3" ) ;

result = "Company List = " + company1 + ", " + company2 + ", " + company3 ;

System . out . println ( result + "\nProgram Ran on " + time + " by user=" + user ) ;

} catch ( Exception e ) {

System . out . println ( "Exception: " + e ) ;

} finally {

inputStream . close ( ) ;

}

return result ;

}

}

Step-4

RunCrunchifyReadConfigMain and checkout result.

Company List = Google , eBay , Yahoo

Program Ran on Mon May 13 21 : 54 : 55 PDT 2013 by user=Crunchify

As usually happy coding and enjoy..!! Do let me know if you see any exception. List of all Java Tutorials.

Are you running above program in IntelliJ IDE and getting NullPointerException?

Please follow below tutorial for fix.

How to add resources folder, properties at runtime into IntelliJ's classpath? Adding property files to classpath

Share:

I'm an Engineer by profession, Blogger by passion & Founder of Crunchify, LLC, the largest free blogging & technical resource site for beginners. Love SEO, SaaS, #webperf, WordPress, Java. With over 16 millions+ pageviews/month, Crunchify has changed the life of over thousands of individual around the globe teaching Java & Web Tech for FREE.

How To Create Config File In Java

Source: https://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/

Posted by: johnsonwhowerromed56.blogspot.com

0 Response to "How To Create Config File In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel