Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在servlet中加载属性文件?_Java_Jsp_Servlets - Fatal编程技术网

Java 在servlet中加载属性文件?

Java 在servlet中加载属性文件?,java,jsp,servlets,Java,Jsp,Servlets,我有一个JavaMaven项目。我在src/main/resources文件夹中放置了一个属性文件 src/main/resources | |___properties | | |___custom_en_US.properties 我正在servlet中加载属性文件,如下所示 ResourceBundle bundle = ResourceBundle.getBundle("classpath:properties/custom",

我有一个JavaMaven项目。我在
src/main/resources
文件夹中放置了一个属性文件

src/main/resources
  |
  |___properties
        |
        |
        |___custom_en_US.properties
我正在servlet中加载属性文件,如下所示

ResourceBundle bundle = ResourceBundle.getBundle("classpath:properties/custom", request.getLocale());
但上面的行抛出异常,表示未找到资源。如何指定属性文件的路径?请帮帮我

谢谢

去掉“classpath”前缀。:
.getBundle(“/properties/custom”)


“classpath”前缀不是标准前缀,它是由一些框架(如spring)定义的。

从classpath读取属性文件

Properties prop = new Properties();

try {
    //load a properties file from class path, inside static method
    prop.load(App.class.getClassLoader().getResourceAsStream("config.properties"));            
} catch (IOException ex) {
    ex.printStackTrace();
}

谢谢你的回复,工作正常。但问题是proeprities文件中有20个键值对。我希望它们以与属性文件中相同的顺序加载。但当我把它们放到servlet中时,它们的顺序就改变了。如何以相同的顺序获取键-值对?它们在属性文件中的顺序如何?谢谢你不应该依赖它,因为它的结构通常是一个哈希表,所以你不能拥有它。如果你想让它们按顺序排列,可以在它们前面加上前缀(1_foo,2_bar),然后按顺序排列。。但通常情况下,订单不必将它们放在html的选择框中。所以我需要把它们整理好。如果有什么我能做的,请建议我?@user755806那么您需要编写自己的代码来读写属性文件。Java属性无法维持秩序。