<tfoot id='S8SlV'></tfoot>

  • <i id='S8SlV'><tr id='S8SlV'><dt id='S8SlV'><q id='S8SlV'><span id='S8SlV'><b id='S8SlV'><form id='S8SlV'><ins id='S8SlV'></ins><ul id='S8SlV'></ul><sub id='S8SlV'></sub></form><legend id='S8SlV'></legend><bdo id='S8SlV'><pre id='S8SlV'><center id='S8SlV'></center></pre></bdo></b><th id='S8SlV'></th></span></q></dt></tr></i><div id='S8SlV'><tfoot id='S8SlV'></tfoot><dl id='S8SlV'><fieldset id='S8SlV'></fieldset></dl></div>
        <bdo id='S8SlV'></bdo><ul id='S8SlV'></ul>
      <legend id='S8SlV'><style id='S8SlV'><dir id='S8SlV'><q id='S8SlV'></q></dir></style></legend>

      1. <small id='S8SlV'></small><noframes id='S8SlV'>

      2. Java 类在 Jar 中读取 Yaml

        时间:2023-09-26

        <small id='1RzI0'></small><noframes id='1RzI0'>

        <tfoot id='1RzI0'></tfoot>
        <legend id='1RzI0'><style id='1RzI0'><dir id='1RzI0'><q id='1RzI0'></q></dir></style></legend>
          <tbody id='1RzI0'></tbody>

        • <bdo id='1RzI0'></bdo><ul id='1RzI0'></ul>

              <i id='1RzI0'><tr id='1RzI0'><dt id='1RzI0'><q id='1RzI0'><span id='1RzI0'><b id='1RzI0'><form id='1RzI0'><ins id='1RzI0'></ins><ul id='1RzI0'></ul><sub id='1RzI0'></sub></form><legend id='1RzI0'></legend><bdo id='1RzI0'><pre id='1RzI0'><center id='1RzI0'></center></pre></bdo></b><th id='1RzI0'></th></span></q></dt></tr></i><div id='1RzI0'><tfoot id='1RzI0'></tfoot><dl id='1RzI0'><fieldset id='1RzI0'></fieldset></dl></div>
                  本文介绍了Java 类在 Jar 中读取 Yaml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试从 jar 中的另一个类读取我的 jar 中的文件.但是我不断收到相同的错误: Caught: class java.io.FileNotFoundException while trying to read metrics: metrics.yml

                  I am trying to read a file inside my jar from another class inside the jar. However I am continually getting the same error: Caught: class java.io.FileNotFoundException while attempting to read metrics: metrics.yml

                  起初我让我的代码做这样的事情,假设它来自类的路径:

                  At first I had my code do something like this, assuming it was from the path of the class:

                  String yamlPath = ".." + File.separator + ".." + File.separator + ".." + File.separator + ".." + File.separator + "myYaml.yml";
                  
                  InputStream in = new FileInputStream(new File(yamlPath));
                  InputStreamReader isr = new InputStreamReader(in);
                  BufferedReader input = new BufferedReader(isr);
                  yamlObj = (HashMap) javaYAML.load(input);
                  

                  我也这样做了,假设它可能会从罐子底部走路径:

                  I also did this assuming it might take the path from the base of the jar:

                  String yamlPath = "myYaml.yml";
                  
                  InputStream in = new FileInputStream(new File(yamlPath));
                  InputStreamReader isr = new InputStreamReader(in);
                  BufferedReader input = new BufferedReader(isr);
                  yamlObj = (HashMap) javaYAML.load(input);
                  

                  然后我注意到了这个线程 How to read a file fromjar in Java? 发现我的路径前需要一个/".我也使用斜线尝试了上述两种方法.

                  I then noticed this thread How to read a file from jar in Java? and found out that I needed a "/" before my path. I tried both methods above using the slash as well.

                  String yamlPath = File.seperator + ".." + File.separator + ".." + File.separator + ".." + File.separator + ".." + File.separator + "myYaml.yml";
                  
                  OR
                  
                  String yamlPath = File.seperator + "myYaml.yml";
                  

                  我现在完全不知道如何处理这个错误.关于 jar 结构有什么我没有得到的吗?为什么找不到我的文件.提前感谢您提供任何帮助/信息.

                  I am completely lost on what to do about this error now. Is there something I am not getting about the jar structure? Why can my file not be found. Thanks in advance for any help / information.

                  抱歉,我忘了提到它在 JAR 中的位置:该类位于以下路径中:com/a/b/c/myclass.classyaml 位于以下路径:myYaml.yml

                  Sorry, I forgot to mention where it is in the JAR: The class is in the following path: com/a/b/c/myclass.class The yaml is in the following path: myYaml.yml

                  推荐答案

                  Jar里面的文件不再是File,用这个改变inputStream的创建

                  File inside Jar is no longer a File, Change inputStream creation with this

                  InputStream in = YourClass.class.getResourceAsStream("myYaml.yml");
                  

                  假设您的 .yml 文件位于 jar 的根目录

                  Assuming your .yml file is at root of jar

                  这篇关于Java 类在 Jar 中读取 Yaml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Spring Boot + Yaml + @PropertySource + @ConfigurationProperti 下一篇:Jackson Yaml 类型信息在序列化时出错

                  相关文章

                  最新文章

                  <small id='dIbQN'></small><noframes id='dIbQN'>

                  <tfoot id='dIbQN'></tfoot>
                    <i id='dIbQN'><tr id='dIbQN'><dt id='dIbQN'><q id='dIbQN'><span id='dIbQN'><b id='dIbQN'><form id='dIbQN'><ins id='dIbQN'></ins><ul id='dIbQN'></ul><sub id='dIbQN'></sub></form><legend id='dIbQN'></legend><bdo id='dIbQN'><pre id='dIbQN'><center id='dIbQN'></center></pre></bdo></b><th id='dIbQN'></th></span></q></dt></tr></i><div id='dIbQN'><tfoot id='dIbQN'></tfoot><dl id='dIbQN'><fieldset id='dIbQN'></fieldset></dl></div>
                    • <bdo id='dIbQN'></bdo><ul id='dIbQN'></ul>
                    <legend id='dIbQN'><style id='dIbQN'><dir id='dIbQN'><q id='dIbQN'></q></dir></style></legend>