我正在尝试创建一个实用程序类
ReadPropertyUtil.java
用于从属性文件读取数据。虽然我的类位于 util 目录下,但我的 skyscrapper.properties
文件放置在其他目录中。
但是,当我尝试使用
[ResourceBundle][1]
访问属性时,出现异常,无法加载该包。
下面是我如何读取属性的代码以及显示我的目录结构的图像。
ReadPropertiesUtil.java
/**
* Properties file name.
*/
private static final String FILENAME = "skyscrapper";
/**
* Resource bundle.
*/
private static ResourceBundle resourceBundle = ResourceBundle.getBundle(FILENAME);
/**
* Method to read the property value.
*
* @param key
* @return
*/
public static String getProperty(final String key) {
String str = null;
if (resourceBundle != null) {
str = resourceBundle.getString(key);
LOGGER.debug("Value found: " + str + " for key: " + key);
} else {
LOGGER.debug("Properties file was not loaded correctly!!");
}
return str;
}
目录结构
此行给出错误
private static ResourceBundle resourceBundle = ResourceBundle.getBundle(FILENAME);
我无法理解为什么这不起作用以及解决方案是什么。
src
文件夹已完全添加到构建路径中。
尝试使用资源的完全限定名称:
private static final String FILENAME = "resources/skyscrapper";
ResourceBundle不加载文件?您需要首先将文件放入资源中。加载到 FileInputStream 然后加载到 PropertyResourceBundle
怎么样? FileInputStream fis = new FileInputStream("skyscrapper.properties");
resourceBundle = new PropertyResourceBundle(fis);
或者如果您需要特定于区域设置的代码,类似这样的代码应该可以工作
File file = new File("skyscrapper.properties");
URL[] urls = {file.toURI().toURL()};
ClassLoader loader = new URLClassLoader(urls);
ResourceBundle rb = ResourceBundle.getBundle("skyscrapper", Locale.getDefault(), loader);
像这样使用资源
ResourceBundle rb = ResourceBundle.getBundle("com//sudeep//internationalization//MyApp",locale);
or
ResourceBundle rb = ResourceBundle.getBundle("com.sudeep.internationalization.MyApp",locale);
只要给出合格的路径..它对我有用!!!
您应该设置不带
.properties
扩展名的属性文件名,
它对我来说工作正常:)
最简单的代码就像,将属性文件保存到资源文件夹中,无论是在 src/main/resource 中还是在 src/test/resource 中。然后使用下面的代码读取属性文件:
public class Utilities {
static {
rb1 = ResourceBundle.getBundle("fileNameWithoutExtension");
// do not use .properties extension
}
public static String getConfigProperties(String keyString) {
return rb1.getString(keyString);
}
}
我想分享我在构建项目时使用 Ant 的经验,*.properties 文件应该显式复制。这是因为 Ant 默认不会将 *.properties 文件编译到构建工作目录中(javac 只是忽略 *.properties)。例如:
<target name="compile" depends="init">
<javac destdir="${dst}" srcdir="${src}" debug="on" encoding="utf-8" includeantruntime="false">
<include name="com/example/**" />
<classpath refid="libs" />
</javac>
<copy todir="${dst}">
<fileset dir="${src}" includes="**/*.properties" />
</copy>
</target>
<target name="jars" depends="compile">
<jar jarfile="${app_jar}" basedir="${dst}" includes="com/example/**/*.*" />
</target>
请注意“编译”目标下的“复制”部分,它将把 *.properties 文件复制到构建工作目录中。如果没有“复制”部分,jar 文件将不包含属性文件,那么您可能会遇到 java.util.MissingResourceException。
使用 Eclipse 和 Windows:
你必须复制2个文件 - xxxPROJECTxxx.properties - log4j.属性 这里:C:\Eclipse\CONTENER\TOMCAT pache-tomcat-7\lib
我刚刚意识到我的错误是由我的属性文件的命名约定引起的。当我使用 xxxx.xxxx.properties 时,我收到错误:
java.util.MissingResourceException:找不到基本名称“property_file name”的包,区域设置 en_US
将其更改为 xxx-xxxx.properties 之类的内容就像一个魅力。希望我能帮助别人!
检查项目构建路径。可能仅包含 *.java 文件。这个问题出现在我的Maven项目中。我需要更改我的 pom.xml 文件。
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
只需右键单击 Eclipse 中的项目文件,然后在构建路径中选择“用作源文件夹”...它对我有用
任何有资源的人都可以尝试-
private static final String FILENAME = "resources.skyscrapper";
private static final String FILENAME = "resources/skyscrapper";
private static final String FILENAME = "resources\\skyscrapper";
private static final String FILENAME = "resources//skyscrapper";
默认情况下,它会尝试在“src”中查找“skyscrapper.properties”文件,但您已将文件放置在无法访问的子目录“resources”中。
在maven项目中-
看起来像 - 'src/main/resources/skyscrapper.properties' 那么 使用-
private static final String FILENAME = "skyscrapper";
如果它看起来像- 'src/main/resources/prop/xyz/skyscrapper.properties' 然后使用-
private static final String FILENAME = "prop/xyz/skyscrapper";
private static final String FILENAME = "prop.xyz.skyscrapper";
在这种情况下,默认情况下它会尝试在“src/main/resources”中查找“skyscrapper.properties”文件,但您的文件实际上位于资源的子目录中,那么您需要提供相对路径,否则您可能会收到类似的异常 -
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name src\main\resources\prop\xyz\skyscrapper.
如果您正在使用 Maven,请尝试以下操作:
public static Connection getConnection(){
//*******************************************************************
try(InputStream input = new FileInputStream("path to file")){
Properties prop = new Properties();
prop.load(input);
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String user = prop.getProperty("user");
String password = prop.getProperty("password");
//Class.forName(driver);
connection = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
我遇到了同样的问题,但是当我将两个(name.properties 或 name.config)文件移动到“NetBeansProjects\projectName arge