title: Read a YAML file in Groovy
body: <p>I am trying to load an existing YAML file (which uses snakeYaml library) in my Groovy project. I have a class called YamlTape.groovy which contains method to load the YAML file using the following code.</p>
  
  <pre><code>static YamlTape readFrom(Reader reader) {
      try {
          println "YamlTape : inside readFrom reader.size() = "+reader+" YamlTape: "+YamlTape
  
          yaml.loadAs(reader, YamlTape)
          println "YamlTape : after readfrom"
      } catch (YAMLException e) {
          println "YamlTape : inside catch block"
          throw new TapeLoadException('Invalid tape', e)
      }
  }
  </code></pre>
  
  <p>and trying to call this method from another groovy class.</p>
  
  <p>Code:</p>
  
  <pre><code>    YamlTape loadTape(String name) {
      println "YamlTapeLoader : inside loadTape"
      def file = fileFor(name)
      println "YamlTapeLoader : inside loadTape filename -name: "+name
      println "YamlTapeLoader : inside loadTape filename -file: "+file
  
      file.setReadable(true);
      file.setWritable(true);
  
      if (file.isFile()) {
          println "YamlTapeLoader : inside file.isFile() : "+file.isFile()
          def tape = file.withReader(FILE_CHARSET) { reader -&gt;
              YamlTape.readFrom(reader)
  
              println "YamlTapeLoader : inside readFrom : "+reader
          }
          println "YamlTapeLoader : tape : "+tape
  
  
          tape
      } else {
          println "YamlTapeLoader : inside ELSE : "
          new YamlTape(name: name)
      }
  }
  </code></pre>
  
  <p>But the tape variable in load tape method always returns null. I have added some logs and found the code is able to access the yaml file but unable to parse Yaml document and return as Java Object. </p>
  
  <p>Logs are : </p>
  
  <pre><code>YamlTapeLoader : inside loadTape
  YamlTapeLoader : inside loadTape filename -name: kar
  YamlTapeLoader : inside loadTape filename -file: /Users/Shared/AATest/Record/kar.yaml
  YamlTapeLoader : inside file.isFile() : true
  YamlTape : inside readFrom reader.size() = java.io.LineNumberReader@34189cab YamlTape: class co.freeside.betamax.tape.yaml.YamlTape
  YamlTape : inside getYaml 
  YamlTape : representer = co.freeside.betamax.tape.yaml.TapeRepresenter@201a503f
  YamlTape : constructor = org.yaml.snakeyaml.constructor.Constructor@16e7eec9
  YamlTape : dumperOption = org.yaml.snakeyaml.DumperOptions@39d7af3
  YamlTape : after readfrom
  YamlTapeLoader : inside readFrom : java.io.LineNumberReader@34189cab
  YamlTapeLoader : tape : null
  </code></pre>