如何在 JAR 文件外部读取或写入 .properties 文件
我创建了一个项目,从 .properties 文件中读取一些配置
public class PreferenceManager {
private int refreshTime;
private String[] filters;
Properties properties ;
public PreferenceManager() throws FileNotFoundException, IOException
{
properties = new Properties();
properties.load(ClassLoader.getSystemResourceAsStream ("preferences.properties"));
}
public void save() throws IOException, URISyntaxException
{
properties.setProperty("REFRESH_TIME", String.valueOf(refreshTime));
String filtersStr = "";
if(filters!= null){
for(String str : filters)
{
if((str == null)||(str.isEmpty())) continue;
filtersStr+= str.toUpperCase()+",";
}
}
properties.setProperty("FILTERS", filtersStr);
URI uri = ClassLoader.getSystemResource("preferences.properties").toURI();
File f = new File(uri);
properties.store(new FileOutputStream(f), null);
}
}
,一切正常。现在我需要创建一个 JAR 文件。我需要知道如何做这个 JAR 文件从包含该属性文件的文件夹中读取该属性文件,因为当属性文件位于 JAR 内时,我可以读取它,但可以在其上写入(例外:URI 不是 hirarchal),
所以我需要您的帮助。
谢谢
I 'v created a project that reads some of configuration from .properties file
public class PreferenceManager {
private int refreshTime;
private String[] filters;
Properties properties ;
public PreferenceManager() throws FileNotFoundException, IOException
{
properties = new Properties();
properties.load(ClassLoader.getSystemResourceAsStream ("preferences.properties"));
}
public void save() throws IOException, URISyntaxException
{
properties.setProperty("REFRESH_TIME", String.valueOf(refreshTime));
String filtersStr = "";
if(filters!= null){
for(String str : filters)
{
if((str == null)||(str.isEmpty())) continue;
filtersStr+= str.toUpperCase()+",";
}
}
properties.setProperty("FILTERS", filtersStr);
URI uri = ClassLoader.getSystemResource("preferences.properties").toURI();
File f = new File(uri);
properties.store(new FileOutputStream(f), null);
}
}
and every thing is ok . now I need to create a JAR file. I need to know how to make this
JAR file read this properties file from the folder that contains it, because when the properties file is inside the JAR I can read it but I can write on it (Exception : URI is not hirarchal)
so I need ur Help please.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将文件存储在用户的主目录中,该目录始终可用,无论是 Windows 还是 Linux/Mac 计算机:
下次,当应用程序启动时,您希望从用户的主目录加载它们当然,如果属性文件存在的话。
比较 https://docs.oracle。 com/javase/8/docs/api/index.html?java/lang/System.html
Simply store the file in the user's home directory, which is always available, be it a Windows or Linux/Mac machine:
Next time, when the application starts you want to load them from user's home directory, of course, if the Properties file exists there.
Compare https://docs.oracle.com/javase/8/docs/api/index.html?java/lang/System.html