java访问.properties中的内容

首先project.properties内容为

oadFile.path=F:\\work\\supplierCollection\\WebContent\\upload

 

然后有一个操作类:

package jy.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 读取properties文件
 * 
 * @author jcicash
 */
public class PropertiesUtil
{
	private static PropertiesUtil instance = new PropertiesUtil();

	private Properties properties;

	private PropertiesUtil()
	{
		InputStream is = null;
		try
		{
			is = this.getClass().getClassLoader().getResourceAsStream("project.properties");
			properties = new Properties();
			properties.load(is);
		}
		catch (FileNotFoundException e)
		{
			System.out.println(e.getMessage());
		}
		catch (IOException e)
		{
			System.out.println(e.getMessage());
		}
		finally
		{
			try
			{
				if (is != null)
				{
					is.close();
				}
			}
			catch (IOException e)
			{
				System.out.println(e.getMessage());
			}
		}
	}

	public static PropertiesUtil getInstance()
	{
		return instance;
	}

	public String getValue(String key)
	{
		return properties.getProperty(key);
	}

	public String getValue(String key, String defaultValue)
	{
		return properties.getProperty(key, defaultValue);
	}
}

 当使用的时候

System.out.println(PropertiesUtil.getInstance().getValue("detailModelFile.path"));

 就可以打印出  F:\\work\\supplierCollection\\WebContent\\upload

你可能感兴趣的:(java访问.properties中的内容)