scala import org.yaml.snakeyaml.Yaml object YamlExample { def main(args: Array[String]): Unit = { val input = getClass.getResourceAsStream("example.yaml") val yaml = new Yaml().load(input) val output = new FileOutputStream("output.yaml") new Yaml().dump(yaml, new OutputStreamWriter(output)) } } scala import org.yaml.snakeyaml.Yaml import org.yaml.snakeyaml.error.YAMLException object YamlErrorHandlingExample { def main(args: Array[String]): Unit = { try { val input = getClass.getResourceAsStream("invalid.yaml") val yaml = new Yaml().load(input) } catch { case ex: YAMLException => } } } scala import org.yaml.snakeyaml.Yaml import java.io.{FileOutputStream, IOException, OutputStreamWriter} object YamlExceptionHandlingExample { @throws(classOf[IOException]) def writeYamlToFile(yaml: AnyRef, filePath: String): Unit = { val output = new FileOutputStream(filePath) new Yaml().dump(yaml, new OutputStreamWriter(output)) } def main(args: Array[String]): Unit = { try { writeYamlToFile(yaml, "output.yaml") } catch { case ex: IOException => } } }


上一篇:
下一篇:
切换中文