Java method for reading properties files

Source: Internet
Author: User

Java method for reading properties files

This example describes how to read the properties file in java. Share it with you for your reference. The specific implementation method is as follows:

?

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

Package com. test. demo;

Import java. util. Properties;

Import java. io. InputStream;

Import java. io. IOException;

/**

* Example of reading the Properties File

* File: TestProperties. java

*/

Public final class TestProperties {

Private static String param1;

Private static String param2;

Private static String param3;

Private static String param4;

/**

* The Path Name of the preceding configuration file is easily overlooked,

* That is, when you use Object. class. getClassLoader (). get,

* Yes, you do not need to add "/".

* But it won't work when getClassLoader () is not needed. Why?

* Because the configuration file is placed under the src of the project, "/" must be added during object loading.

* If you copy the configuration file to the same package as the class, you do not need to add it,

* If the configuration file is read in the following way:

*/

Static {

Properties prop = new Properties ();

// InputStream in = Object. class. getResourceAsStream ("/test. properties ");

// File under src

// InputStream in = TestProperties. class. getClassLoader (). getResourceAsStream ("jdbc. properties ");

// File under src

InputStream in = TestProperties. class. getResourceAsStream ("jdbc. properties ");

// The file is in the same package

Try {

Prop. load (in );

Param1 = prop. getProperty ("mysql. driverClassName"). trim ();

Param2 = prop. getProperty ("mysql. url"). trim ();

Param3 = prop. getProperty ("mysql. username"). trim ();

Param4 = prop. getProperty ("mysql. password"). trim ();

} Catch (IOException e ){

E. printStackTrace ();

}

}

/**

* The private constructor does not need to create objects.

*/

Private TestProperties (){

}

Public static String getParam1 (){

Return param1;

}

Public static String getParam2 (){

Return param2;

}

Public static String getParam3 (){

Return param3;

}

Public static String getParam4 (){

Return param4;

}

Public static void main (String args []) {

System. out. println (getParam1 ());

System. out. println (getParam2 ());

System. out. println (getParam3 ());

System. out. println (getParam4 ());

}

}

I hope this article will help you with java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.