有什么方法可以更改类路径中的属性文件吗?

发布于 2024-12-10 12:14:54 字数 205 浏览 0 评论 0原文

我的类路径中有一个属性文件。我的要求是根据我从参数中获得的一些输入更改此文件中的一些属性。参数的性质决定我是否需要更改属性以及如果需要更改哪些属性。问题是所有类路径条目都是在应用程序启动时加载的,因此从我的应用程序内部进行更改不会产生任何效果。我该如何克服这个问题?

我能想到的一种可能的解决方案是不在类路径中添加此属性文件,而是在修改完成后添加。它可行吗?有什么好的解决办法吗?

I have a properties file that is in my classpath. My requirement is to change some properties in this file based on some inputs I get from arguments. The nature of arguments decide whether I need to change the properties and if yes which properties to change. The problem is that all classpath entries are loaded at the application startup time, so changing from within my application would not have any effect. How do I overcome this problem?

One possible solution I can think of is to not add this properties file in classpath but add after modifications are done. Is it viable? What can be a good solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡紫姑娘! 2024-12-17 12:14:54

该文件是否位于您的类路径中并不重要。它是一个文件:如果您覆盖其内容,它就会发生变化。启动时并没有神奇地生成一些内存中的副本。这与加载的类非常不同,类可能需要在运行时进行更改。

遵循正确格式的属性文件可以读入 java.util.Properties 对象。您可以这样做,使用该对象根据需要更改属性,然后将其写回到文件中。检查该类中的 storeload 方法。请注意,如果您使用采用 Output/InputStream 的版本,则编码是硬编码的。如果文件的编码不是 ISO-8859-1,请使用具有适当 Writer/Reader 的方法。

It doesn't matter whether this file is on your classpath or not. It is a file: if you overwrite its contents, it will have changed. There isn't some in-memory copy that magically gets made at startup. This is very different from classes that are loaded in and which might need change at runtime.

Properties files that adhere to the right format can be read into a java.util.Properties object. You could do that, use the object to alter the properties as needed, then write it back out to the file. Check the store and load methods in that class. Mind that if you use the versions that take an Output/InputStream, the encoding is hard-coded. If the file's encoding is anything else than ISO-8859-1, use a method with an appropriate Writer/Reader.

上课铃就是安魂曲 2024-12-17 12:14:54

取决于您的应用程序的部署方式。如果您的属性文件位于 jar 内,则您将无法直接更改该属性文件,因为它已打包并压缩在存档中。您可以像其他人提到的那样将这些属性加载到对象中,然后存储/写入到外部位置,可能是基于 URL 的位置。 URL 很方便,因为它可以让您访问几乎任何位置,并且它具有用于加载属性的漂亮的 openStream() 方法。然后,您的应用程序可以在加载时查找新文件,如果无法从新位置读取/加载,则默认为应用程序启动版本。

Depends on how your application is deployed. If your properties files is inside a jar, you won't be able to directly change that properties file since its packaged and zipped up in an archive. You can instead as someone else mentioned load those properties into an object, and then store/write out to an external location, probably a URL based location. URL is convenient because it gets you access to virtually any location and it has that nifty openStream() method for loading properties. Your application could then look for the new file on load, and default to the application startup version if it fails to read/load from the new location.

风柔一江水 2024-12-17 12:14:54

这是示例代码:

Properties p = new Properties();
File f = new File("file");
InputStream in = new FileInputStream(f);
p.load(in);
p.put("key", "blah");

OutputStream out = new FileOutputStream(f);
// If no comments p.store(writer);
p.store(out, "properties");

Here is a sample code:

Properties p = new Properties();
File f = new File("file");
InputStream in = new FileInputStream(f);
p.load(in);
p.put("key", "blah");

OutputStream out = new FileOutputStream(f);
// If no comments p.store(writer);
p.store(out, "properties");
七月上 2024-12-17 12:14:54

您需要首先从属性文件中删除该属性,然后重新定义它。他们没有办法直接修改属性文件。

下面是一个例子:

   Properties pproperties = new Properties();

    if (properties.containsKey("key1")) {

                        properties.remove("key1");
                        properties.setProperty("key1", "value1");
                        properties.store(new FileOutputStream("file.properties"), null);
                    }

You need to first remove that property from the property file and then re-define it. Their is no way to directly modify the properties file.

Below is an example:

   Properties pproperties = new Properties();

    if (properties.containsKey("key1")) {

                        properties.remove("key1");
                        properties.setProperty("key1", "value1");
                        properties.store(new FileOutputStream("file.properties"), null);
                    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文