JavaFX项目打包后,读取修改配置文件

最近写了一个JavaFX小程序,需要能够动态的修改配置文件。但是发现两个问题:
1)打包后有一些资源文件比如word、excel没法读取
2)配置文件如果在打的jar包内,则没法进行修改
我自己解决方法是:将配置文件移到jar包同一个文件夹内,然后先通过虚拟机获取jar包位置后,再获取配置文件的路径

获取路径的代码,我是看这篇文章的。
具体代码如下,包含两个类:

public class YamlUtils {
    private static final String PATH = PathUtils.getJarParentPath() + "path.yaml";
    public static PathConfiguration getPathConfiguration() throws FileNotFoundException {
        Yaml yaml = new Yaml();
        FileReader fileReader = new FileReader(PATH);
        return yaml.load(fileReader);
    }
    public static void savePathConfiguration(String pendingDirectoryPath, String targetDirectoryPath) throws IOException {
        PathConfiguration pathConfiguration = new PathConfiguration(pendingDirectoryPath, targetDirectoryPath);
        Yaml yaml = new Yaml();
        FileWriter fileWriter = new FileWriter(PATH);
        yaml.dump(pathConfiguration, fileWriter);
    }
}


public class PathUtils {
    public static String getJarParentPath() {
        String path = System.getProperty("java.class.path");
        int firstIndex = path.lastIndexOf(System.getProperty("path.separator")) + 1;
        int lastIndex = path.lastIndexOf(File.separator) + 1;
        return path.substring(firstIndex, lastIndex);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值