Java read properties when the path to pay attention to the problem,

Source: Internet
Author: User

Let's take a look at the established Test Engineering catalogue



Property file We put it under the package test, of course, generally in the actual development process is not recommended to do so, the establishment of the attribute file in the SRC directory, now placed under the package is mainly easy to understand the path of the problem.

Let's take a look at the code that reads the property file, and the property file configures a class Hello K-v key value that we're going to read and load into memory.

Readproperties.properties

V=com.luhy.test.hello


Hello class:

Package com.luhy.test;

public class Hello {public
	void run () {
		System.out.println ("Hello");
	}


Readproperties.java

Package com.luhy.test;

Import java.util.Properties;

public class ReadProperties {public
	static void Main (string[] args) throws exception{
		String filename = "Com/luhy /test/readproperties.properties ";
		Properties Props = new properties ();
		Props.load (ReadProperties.class.getClassLoader (). getResourceAsStream (filename));
		String h = props.getproperty ("V");
		Object o = Class.forName (h). newinstance ();
		Hello hello = (hello) o;
		Hello.run ();
		
	}


Finish Print out:

Hello

Let's look at the compiled bin directory


Visible after the compilation of the property file is automatically placed in the appropriate package, of course, where the bin is equivalent to the source in the SRC, the actual development in general placed in this SRC directory, so in the release of the project will not have to toss.

Description

1, Props.load (ReadProperties.class.getClassLoader (). getResourceAsStream (filename)); It means to get the class loader from the properties class (the class loader has four main types, loading different classes separately, loading the class file into memory and not producing objects), and converting the specified file into a stream. This step, there are a lot of novice, directly to the load () fill in the file name or file path name, the program will not be able to run error found the specified path. Therefore, we must pay attention to this point.

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.