Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SnakeYAML:不';好像不行_Java_Yaml_Snakeyaml - Fatal编程技术网

Java SnakeYAML:不';好像不行

Java SnakeYAML:不';好像不行,java,yaml,snakeyaml,Java,Yaml,Snakeyaml,以下是我提到的教程- 我的代码看起来像 public class Utilities { private static final String YAML_PATH = "/problems/src/main/resources/input.yaml"; public static Map<String, Object> getMapFromYaml() { Yaml yaml = new Yaml(); Map<String,

以下是我提到的教程-

我的代码看起来像

public class Utilities {
    private static final String YAML_PATH = "/problems/src/main/resources/input.yaml";

    public static Map<String, Object> getMapFromYaml() {
        Yaml yaml = new Yaml();
        Map<String, Object> map = (Map<String, Object>) yaml.load(YAML_PATH);
        System.out.println(map);
        return map;
    }

    public static void main(String args[]) {
        getMapFromYaml();
    }
}  
当我运行程序时,我看到

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
    at com.ds.utilities.Utilities.getMapFromYaml(Utilities.java:19)
    at com.ds.utilities.Utilities.main(Utilities.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 1
如何修复此问题以使其正常工作?

它工作正常

public class RuntimeInput {
    private final Map<String, Object> RUNTIME_INPUT;

    private static final String SORTING = "sorting";
    private static final String YAML_PATH = "/src/main/resources/input.yaml";


    public RuntimeInput() {
        RUNTIME_INPUT = getMapFromYaml();
    }

    public static Map<String, Object> getMapFromYaml() {
        Yaml yaml = new Yaml();
        Reader reader = null;
        Map<String, Object> map = null;
        try {
            reader = new FileReader(YAML_PATH);
            map = (Map<String, Object>) yaml.load(reader);
        } catch (final FileNotFoundException fnfe) {
            System.err.println("We had a problem reading the YAML from the file because we couldn't find the file." + fnfe);
        } finally {
            if (null != reader) {
                try {
                    reader.close();
                } catch (final IOException ioe) {
                    System.err.println("We got the following exception trying to clean up the reader: " + ioe);
                }
            }
        }
        return map;
    }

    public Map<String, Object> getSortingDataInput() {
        return (Map<String, Object>) RUNTIME_INPUT.get(SORTING);
    }

    public static void main(String args[]) {
        RuntimeInput runtimeInput = new RuntimeInput();
        System.out.println(Arrays.asList(runtimeInput.getSortingDataInput()));
    }
}
公共类运行时输入{
私有最终映射运行时_输入;
私有静态最终字符串排序=“排序”;
私有静态最终字符串YAML_PATH=“/src/main/resources/input.YAML”;
公共运行时输入(){
运行时输入=getMapFromYaml();
}
公共静态映射getMapFromYaml(){
Yaml Yaml=新的Yaml();
Reader=null;
Map=null;
试一试{
reader=新文件读取器(YAML_路径);
map=(map)yaml.load(读卡器);
}捕获(最终文件NotFoundException fnfe){
System.err.println(“我们从文件中读取YAML时遇到问题,因为我们找不到文件。”+fnfe);
}最后{
if(null!=读取器){
试一试{
reader.close();
}捕获(最终ioe异常ioe){
System.err.println(“我们在清理读卡器时遇到以下异常:“+ioe”);
}
}
}
返回图;
}
公共地图getSortingDataInput(){
return(Map)RUNTIME_INPUT.get(排序);
}
公共静态void main(字符串参数[]){
RuntimeInput RuntimeInput=新的RuntimeInput();
System.out.println(Arrays.asList(runtimeInput.getSortingDataInput());
}
}
public class RuntimeInput {
    private final Map<String, Object> RUNTIME_INPUT;

    private static final String SORTING = "sorting";
    private static final String YAML_PATH = "/src/main/resources/input.yaml";


    public RuntimeInput() {
        RUNTIME_INPUT = getMapFromYaml();
    }

    public static Map<String, Object> getMapFromYaml() {
        Yaml yaml = new Yaml();
        Reader reader = null;
        Map<String, Object> map = null;
        try {
            reader = new FileReader(YAML_PATH);
            map = (Map<String, Object>) yaml.load(reader);
        } catch (final FileNotFoundException fnfe) {
            System.err.println("We had a problem reading the YAML from the file because we couldn't find the file." + fnfe);
        } finally {
            if (null != reader) {
                try {
                    reader.close();
                } catch (final IOException ioe) {
                    System.err.println("We got the following exception trying to clean up the reader: " + ioe);
                }
            }
        }
        return map;
    }

    public Map<String, Object> getSortingDataInput() {
        return (Map<String, Object>) RUNTIME_INPUT.get(SORTING);
    }

    public static void main(String args[]) {
        RuntimeInput runtimeInput = new RuntimeInput();
        System.out.println(Arrays.asList(runtimeInput.getSortingDataInput()));
    }
}