1. Create a directory in the same directory as SRC called resources and place the config file config.properties:4

#Crunchify Properties user=Jerry Company1 =Google Company2 =eBay company3=Yahoo

  1. Use the following Java code to read the configuration file:
package partner1;

import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

public class Test {

	public void work(a){
		InputStream input = null;
		
		Properties prop = new Properties();
		String propFileName = "config.properties";

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

		if(input ! =null) {
			try {
				prop.load(input);
				int available = input.available();
				System.out.println("Available: " + available);
				String user = prop.getProperty("user");
				String company1 = prop.getProperty("company1");
				String company2 = prop.getProperty("company2");
				String company3 = prop.getProperty("company3");
	 
				Date time = new Date(System.currentTimeMillis());
				String result = "Company List = " + company1 + "," + company2 + "," + company3;
				System.out.println(result + "\nProgram Ran on " + time + " by user=" + user);
				input.close();
			} catch (Exception e) {
				System.out.println("Exception: " + e);
			} finally{}}}public static void main(String[] args) {
		Test test = newTest(); test.work(); }}Copy the code

Note that the Resources folder needs to be set to build path:


      
<classpath>
	<classpathentry excluding="resources/" kind="src" path="src"/>
	<classpathentry kind="src" path="resources"/>
Copy the code

Execution Result:

For more of Jerry’s original articles, please follow the public account “Wang Zixi “: