Specific steps to read a properties file in a Java project

Source: Internet
Author: User
Tags parent directory trim

The following 1-4 of the content is online collection of relevant knowledge, summed up, is the following several knowledge points:

1. The most common way to read properties files InputStream in = GetClass (). getResourceAsStream ("Resource Name"), which requires the properties file to be under the same folder as the current class. If you are in a different package, you must use:

InputStream ins =this.getclass (). getResourceAsStream ("/cn/zhao/properties/testpropertiespath2.properties");

2. Get path method in Java

3, to obtain a simple implementation of the path

4, reflect the way to obtain properties of the file three ways

1 The most common methods of retrieving properties files from Reflective mode and thinking:

Java read the properties file more methods, the most online article is "Java read properties file six Ways", but in Java applications, It is most often done by the getResourceAsStream (String name) method of the Java.lang.Class class, but I see many of the code that reads the properties file:

InputStream in = GetClass (). getResourceAsStream ("Resource Name");

There is a problem in this, that is, the GetClass () call by default omitted this! As we all know, this cannot be used in static (static) methods or static blocks because the static type method or block of code belongs to the class itself, not to an object, and this itself represents the current object, Static methods or block calls are not initialized with the object.

The problem is this: if I don't want a class to have an object, I'll make the default constructor for this class private, and of course not write any other common construction methods. And my class is a tool class, are static methods and variables, I want to be in the static block or static method to get the properties file, this method is not feasible.

What about it? In fact, this class is not so used, he just need to get a class object on it, it is not easy AH-
To take all classes of the parent object, with Object.class is not more than your use you are writing classes themselves convenient security? Hehe, here is an example to facilitate communication.

Importjava.util.Properties;
Importjava.io.InputStream;
Importjava.io.IOException;
/**
* Examples of reading properties files
* File:TestProperties.java
* User:leizhimin
* Date:2008-2-15 18:38:40
*/
Public final Classtestproperties {
Private staticstring param1;
Private staticstring param2;
static{
Properties prop =newproperties ();
InputStream in = Object.class.getResourceAsStream ("/test.properties");
try{
Prop.load (in);
param1 = Prop.getproperty ("initYears1"). Trim ();
param2 = Prop.getproperty ("InitYears2"). Trim ();
}catch (IOException e) {
E.printstacktrace ();
}
}
/**
* Private construction method, no need to create object
*/
Privatetestproperties () {
}
Public staticstring getParam1 () {
returnparam1;
}
Public staticstring getParam2 () {
RETURNPARAM2;
}
public static Voidmain (String args[]) {
System.out.println (GETPARAM1 ());
System.out.println (GetParam2 ());
}
}

Run Result:

151
152

Of course, the Object.class replaced Int.class so do, oh, we can try.

In addition, if the static method or block to read the properties file, there is one of the most insurance method, is the name of the class itself to get the class object directly, such as this example can be written in Testproperties.class, this is the most insurance method

2 How to get the path:

File Fileb =newfile (This.getclass (). GetResource (""). GetPath ());
System. Out. println ("Fileb path:" + Fileb);

2.2 Get the project name where the current class is located:

System. Out. println ("User.dir path:" + System. GetProperty ("User.dir"))

3 Getting a simple Java implementation of the path

/**
* Get the absolute path of the file under the relative path of the project
*
* @param parentdir
* Target file's parent directory, for example, under the project directory, there are Lib and bin and conf directory, then the program runs in the Lib or
* Bin, then the required configuration file is conf inside, you need to find the absolute path of the configuration file
* @param fileName
* File name
* @return an absolute path
*/
Publicstaticstring GetPath (String parentdir, String fileName) {
String path =null;
String Userdir = System.getproperty ("User.dir");
String userdirname =newfile (userdir). GetName ();
if (Userdirname.equalsignorecase ("Lib")
|| Userdirname.equalsignorecase ("Bin")) {
File Newf =newfile (Userdir);
File newp =newfile (Newf.getparent ());
if (Filename.trim (). Equals ("")) {
Path = Newp.getpath () + File.separator + parentdir;
}else{
Path = Newp.getpath () + File.separator + parentdir
+ File.separator + fileName;
}
}else{
if (Filename.trim (). Equals ("")) {
Path = Userdir + File.separator + parentdir;
}else{
Path = Userdir + file.separator + parentdir + file.separator
+ FileName;
}
}
Returnpath;
}

4 Get the path using reflection:

inputstream ips1 = Enumer ation. Class.getclassloader (). getResourceAsStream ("cn/zhao/enumstudy/testpropertiespath1.properties");
InputStream IPS2 = enumeration. Class.getresourceasstream ("testpropertiespath1.properties");
InputStream IPS3 = enumeration. Class.getresourceasstream ("Properties/test
Related Article

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.