小编Jac*_*cob的帖子

如何在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);
    }
}
Run Code Online (Sandbox Code Playgroud)

一切都很好.现在我需要创建一个JAR文件.我需要知道如何让这个JAR文件从包含它的文件夹中读取这个属性文件,因为当属性文件在JAR中时我可以读它但我可以在上面写(例外:URI不是hirarchal)

所以我需要你的帮助.

谢谢

java jar properties

1
推荐指数
1
解决办法
7913
查看次数

标签 统计

jar ×1

java ×1

properties ×1