2012-07-02 4 views
0

Я следую учебник упоминается - http://code.google.com/p/snakeyaml/wiki/Documentation#TutorialSnakeYAML: не похоже на работу

Мой код выглядит как

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(); 
    } 
} 

мой YAML файл выглядит

sorting 
    01: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] 

Когда я запускаю мою программу Я вижу

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 

Как я могу исправить это, чтобы он работал?

ответ

-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())); 
    } 
} 
Смежные вопросы