我无法在NetBeans中向我的类路径添加自定义file.properties。这是我的根目录:https://bpaste.net/raw/379b695f7a12
我创建了一个资源目录,其中放置了我的所有属性文件
/ src / java< - root classpath文件夹 / src / resources< - 属性文件(如dao.properties)
如果我将dao.properties放在根类路径中,我可以加载它: /src/java/dao.properties
如果我把dao.properties放在任何其他subpkg中,它将无法找到(classpath error)
依旧......
使用Bauke Scholtz提供的课程来加载属性:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dao;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* This class immediately loads the DAO properties file 'dao.properties'
* once in memory and provides a constructor which takes the specific key
* which is to be used as property key prefix of the DAO properties file.
* There is a property getter which only returns the property prefixed
* with 'specificKey.' and provides the option to indicate whether the
* property is mandatory or not.
*
* @author BalusC
* @link http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html
*/
public class DAOProperties {
// Constants ------------------------------------------------------
private static final String PROPERTIES_FILE = "dao.properties";
private static final Properties PROPERTIES = new Properties();
static {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream propertiesFile = classLoader.getResourceAsStream(PROPERTIES_FILE);
if (propertiesFile == null) {
throw new DAOConfigurationException(
"Properties file '" + PROPERTIES_FILE + "' is missing in classpath.");
}
try {
PROPERTIES.load(propertiesFile);
} catch (IOException e) {
throw new DAOConfigurationException(
"Cannot load properties file '" + PROPERTIES_FILE + "'.", e);
}
}
// Vars -----------------------------------------------------------
private String specificKey;
// Constructors ---------------------------------------------------
/**
* Construct a DAOProperties instance for the given specific key which
* is to be used as property key prefix of the DAO properties file.
*
* @param specificKey The specific key which is to be used as property
* key prefix.
* @throws DAOConfigurationException During class initialization if the
* DAO properties file is missing in the classpath or cannot be loaded.
*/
public DAOProperties(String specificKey) throws DAOConfigurationException {
this.specificKey = specificKey;
}
// Actions --------------------------------------------------------
/**
* Returns the DAOProperties instance specific property value associated
* with the given key with the option to indicate whether the property is
* mandatory or not.
*
* @param key The key to be associated with a DAOProperties instance
* specific value.
* @param mandatory Sets whether the returned property value should not be
* null nor empty.
* @return The DAOProperties instance specific property value associated
* with the given key.
* @throws DAOConfigurationException If the returned property value is null
* or empty while it is mandatory.
*/
public String getProperty(String key, boolean mandatory)
throws DAOConfigurationException {
String fullKey = specificKey + "." + key;
String property = PROPERTIES.getProperty(fullKey);
if (property == null || property.trim().length() == 0) {
if (mandatory) {
throw new DAOConfigurationException(
"Required property '" + fullKey + "'" +
" is missing in properties file '" +
PROPERTIES_FILE + "'.");
} else {
// Make empty value null. Empty Strings are evil.
property = null;
}
}
return property;
}
}
在此先感谢您的支持,我整天都在与Google和netbeans争夺答案。