【问题标题】:Read config.properties file from normal Folder and NOT SrcFolder (Java)从普通文件夹而不是 SrcFolder (Java) 读取 config.properties 文件
【发布时间】:2022-01-13 22:22:20
【问题描述】:

如何从 Maven 的普通文件夹中读取 config.properties 文件。

我的代码所在的位置是 src/main/java 的正常 Maven 结构。我还有另一个文件夹 config 我的 config.properties 文件所在的位置。

现在我的问题。我想使用 Instream 访问 config.properties,但如果我这样做,我会返回 Null。如果我在 SrcFolder 中执行 config.properties 它就像一个魅力。那我该怎么做呢?

我想通过 Instream 执行此操作,因为我使用 prop.get

[我的文件夹结构][1] [1]:https://i.stack.imgur.com/669xT.png

我的代码用于读取我的 config.properties,我得到 Null 作为返回。

import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

// import org.apache.log4j.Logger;
// import org.apache.log4j.PropertyConfigurator;

public class PropertyManager {

    // private static Logger logger = Logger.getLogger(PropertyManager.class);

    private static final String PROPERTIES_FILE = "config.properties";
    private Properties props = null;
    File file = new File("config\\config.properties");

    public PropertyManager() throws IOException {

        if (file.createNewFile()) {
            System.out.println("Neue config.propertie Datei wurde angelegt, mit standart Werte.");
            final FileWriter writer = new FileWriter(file);
            writer.write("input.file=Input\\\\berufe.csv");
            writer.close();
            System.out.println("Programm wurde beendet, bitte erneut ausführen");
            System.exit(0);
        } else {
            System.out.println("config.propertie Datei ist vorhanden.");
            loadProperties(PROPERTIES_FILE);
        }
    }

    private void loadProperties(String filename) throws IOException {

        props = new Properties();

        final ClassLoader loader = this.getClass().getClassLoader();

        try (InputStream instream = loader.getResourceAsStream(filename)) {

            if (instream == null) {
                System.out.println("Property Datei kann nicht geöffnet werden.");
                return;
            }

            try {
                props.load(instream);
            } catch (final Exception e) {
                System.out.println("Error beim lesen der Property Datei");
                // logger.error("Error beim lesen der Property Datei");
            }

        }

    }

    public String getProperty(String name) {

        return props.getProperty(name);
    }
}```

In my config.propertie file are lines. It is not empty.

【问题讨论】:

  • Class#getResourceAsStream(String) - 如果config.propertiessrc/main/resources 目录中。如果不是(即它是外部化的),那么您会遇到与应用上下文之外的查找和资源相关的所有其他问题
  • 但如果我只是创建一个 Srcfolder 而不是一个普通文件夹,它就可以正常工作,并且 Srcfolder 不需要位于 src/main/java 中
  • 您缺少上下文。如果文件位于应用程序上下文之外(即它没有嵌入),那么您可能会遇到问题,如果工作目录与文件的位置不同(即您从不同的目录运行应用程序),这就是它的原因鼓励使用嵌入式资源(用于只读内容)。对于可写内容,它们应该放置在“众所周知的位置”。每个操作系统对如何处理都有不同的要求
  • 或许可以试试:final String fs = File.separator; File file = new File(new File("").getAbsolutePath() + fs + "config" + fs + "config.properties");.
  • 仍然是“java.lang.NullPointetException”

标签: java maven


【解决方案1】:

解决方案!

我需要它说 PROPERTIES_FILE = „confog\confog.properties“ 并尝试 (InputStream instream = new FileInputStream(filename)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多