Using properties file in java application

Properties files are a cool place to store your configuration details like database connection properties, error messages and other configurations for your program.

You can use properties file in your application using following utility class. This class basically loads your properties file on first attempt to access any property on further attempts to access any property it will return properties already loaded in memory and not attempt to read file again and again.

Static getProperty(String key) method allows you to get property value by passing the key and static method Set<Object> getkeys() returns a set of keys in the file.

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

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

package com.bharat.utils;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

import java.util.Set;

public class PropertiesUtil

{

private static Properties props;

static

{

props = new Properties();

try

{

PropertiesUtil util = new PropertiesUtil();

props = util.getPropertiesFromClasspath("placeholder.properties");

}

catch (FileNotFoundException e)

{

e.printStackTrace();

}

catch (IOException e)

{

e.printStackTrace();

}

}

// private constructor

private PropertiesUtil()

{

}

public static String getProperty(String key)

{

return props.getProperty(key).trim();

}

public static Set getkeys()

{

return props.keySet();

}

/**

* loads properties file from classpath

*

* @param propFileName

* @return

* @throws IOException

*/

private Properties getPropertiesFromClasspath(String propFileName)

throws IOException

{

Properties props = new Properties();

InputStream inputStream =

this.getClass().getClassLoader().getResourceAsStream(propFileName);

if (inputStream == null)

{

throw new FileNotFoundException("property file ‘" + propFileName

+ "‘ not found in the classpath");

}

props.load(inputStream);

return props;

}

}

You may put your properties file anywhere in project’s classpath.

References:

  1. http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html
  2. http://bharatonjava.wordpress.com/2012/09/12/using-properties-file-in-java-application/
时间: 2024-10-29 10:48:25

Using properties file in java application的相关文章

java 读取配置文件工具类 (how to read values from properties file in java)

Java 读取配置文件工具类 使用 java.util.Properties import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiesReader { private static Properties prop; static { reload(); } private static void reload() { prop = new

java get properties file

// String title = "行政院农业委员会农粮署"; ? ? ? ? InputStream inputStream = this.getClass().getClassLoader() ? ? ? ? ? ? ? ? .getResourceAsStream("Messages.properties"); ? ? ? ? Properties properties = new Properties(); ? ? ? ? // load the inpu

win10+eclipse+hadoop2.7.2+maven直接通过Run as Java Application运行wordcount

一.准备工作 (1)Hadoop2.7.2 在linux部署完毕,成功启动dfs和yarn,通过jps查看,进程都存在 (2)安装maven 二.最终效果 在windows系统中,直接通过Run as Java Application运行wordcount,而不需要先打包成jar包,然后在linux终端运行 三,操作步骤 1.启动dfs和yarn 终端:${HADOOP_HOME}/sbin/start-dfs.sh ${HADOOP_HOME}/sbin/start-yarn.sh 通过在na

How to decompile class file in Java and Eclipse - Javap command example(转)

Ability to decompile a Java class file is quite helpful for any Java developer who wants to look into the source of any open source or propriety library used in project. Though I always prefer to attach source in Eclipse of most common libraries like

JMeter-MyEclipse编译运行问题(Could not read JMeter properties file)

JMeter-MyEclipse编译运行问题 按照 此贴 http://phoenix0529.iteye.com/blog/1530728 进行配置,然后用Ant编译Build.xml 是可以的. 但是如果run newdriver 就报错,如下: java.lang.Throwable: Could not access E:\Workspaces\MyEclipse 10\libat org.apache.jmeter.NewDriver.<clinit>(NewDriver.java:

The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object

The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object (2007-08-29 10:13:56) 转载▼ 错误提示:The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object

【Properties文件】Java使用Properties来读取配置文件

配置文件位置及内容 执行结果 程序代码 package Utils.ConfigFile;   import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Enu

Read / Write Excel file in Java using Apache POI

Read / Write Excel file in Java using Apache POI 2014-04-18 BY DINESH LEAVE A COMMENT About a year or two ago I was working with finance team where they wanted to pull the credit card transactions for all the customer using various combinations. Ex –

How to Read, Write XLSX File in Java - Apach POI Example---reference

No matter how Microsoft is doing in comparison with Google, Microsoft Office is still the most used application in software world. Other alternatives like OpenOffice and LiberOffice have failed to take off to challenge MS Office. What this mean to a