在.yml配置文件中配置绝对路径可以使用以下格式:

path:
  absolute: /path/to/my/file.txt

在Java中调用该配置文件并读取绝对路径可以使用以下代码:

import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) {
        Yaml yaml = new Yaml();
        try (InputStream in = Files.newInputStream(Paths.get("config.yml"))) {
            Config config = yaml.loadAs(in, Config.class);
            String filePath = config.getPath().getAbsolute();
            System.out.println(filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Config {
    private Path path;

    public Path getPath() {
        return path;
    }

    public void setPath(Path path) {
        this.path = path;
    }
}

class Path {
    private String absolute;

    public String getAbsolute() {
        return absolute;
    }

    public void setAbsolute(String absolute) {
        this.absolute = absolute;
    }
}

这里使用SnakeYAML库读取配置文件,并将配置文件的内容映射到Java对象中。然后可以通过Java对象获取配置文件中的绝对路径

Java在yml配置文件配置绝对路径在文件中调用

原文地址: https://www.cveoy.top/t/topic/fd4X 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录