如何在 JAR 文件外部读取或写入 .properties 文件

发布于 2024-11-28 01:51:54 字数 1189 浏览 1 评论 0原文

我创建了一个项目,从 .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 技术交流群。

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

发布评论

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

评论(1

帅的被狗咬 2024-12-05 01:51:54

只需将文件存储在用户的主目录中,该目录始终可用,无论是 Windows 还是 Linux/Mac 计算机:

// Initially load properties from jar
Properties props = new Properties();
properties.load(ClassLoader.getSystemResourceAsStream ("preferences.properties"));

// .. work with properties

// Store them in user's home directory
File userHome = new File(System.getProperty("user.home"));
File propertiesFile = new File(userHome, "preferences.properties");

props.store(new FileOutputStream(propertiesFile, "Properties for MyApp");

下次,当应用程序启动时,您希望从用户的主目录加载它们当然,如果属性文件存在的话。
比较 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:

// Initially load properties from jar
Properties props = new Properties();
properties.load(ClassLoader.getSystemResourceAsStream ("preferences.properties"));

// .. work with properties

// Store them in user's home directory
File userHome = new File(System.getProperty("user.home"));
File propertiesFile = new File(userHome, "preferences.properties");

props.store(new FileOutputStream(propertiesFile, "Properties for MyApp");

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文