YAML的Java實現——JYAML基本原理與示例(3)YAML對檔案流的處理
請您先閱讀:
《YAML的Java實現——JYAML基本原理與示例(1)匯出資料為YAML格式檔案》
《YAML的Java實現——JYAML基本原理與示例(2)匯入YAML格式檔案》
1. FileOutputStream
以流的方式,將資料寫入到YAML檔案中。
/* Output data structure into a YAML file as a FileOutputStream. */
try {
YamlEncoder yEncoder = new YamlEncoder(new FileOutputStream(dumpFile));
for (int i = 0; i < 3; ++i) {
michael.setAge(24 + i);
yEncoder.writeObject(michael);
yEncoder.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
2. FileInputStream
以流的方式,從YAML檔案中將資料讀入。
/* Input a YAML file into data structure as a FileOutputStream. */
try {
YamlDecoder yDecoder = new YamlDecoder(new FileInputStream(dumpFile));
Person[] persons = {new Person(), new Person(), new Person()};
for (int i = 0; i < 3; ++i) {
persons[i] = (Person) yDecoder.readObject();
System.out.println();
TestYaml.output(persons[i]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (EOFException e) {
e.printStackTrace();
}
3. 檢視YAML檔案
--- &0 !com.sinosuperman.yaml.Person
age: 24
children: &2 !com.sinosuperman.yaml.Person[]
- !com.sinosuperman.yaml.Person
age: 3
name: boy
- !com.sinosuperman.yaml.Person
age: 1
name: girl
name: Michael Corleone
spouse: !com.sinosuperman.yaml.Person
age: 24
children: *2
name: Floveria Edie
spouse: *0
--- &9 !com.sinosuperman.yaml.Person
age: 25
children: &11 !com.sinosuperman.yaml.Person[]
- !com.sinosuperman.yaml.Person
age: 3
name: boy
- !com.sinosuperman.yaml.Person
age: 1
name: girl
name: Michael Corleone
spouse: !com.sinosuperman.yaml.Person
age: 24
children: *11
name: Floveria Edie
spouse: *9
--- &18 !com.sinosuperman.yaml.Person
age: 26
children: &20 !com.sinosuperman.yaml.Person[]
- !com.sinosuperman.yaml.Person
age: 3
name: boy
- !com.sinosuperman.yaml.Person
age: 1
name: girl
name: Michael Corleone
spouse: !com.sinosuperman.yaml.Person
age: 24
children: *20
name: Floveria Edie
spouse: *18
相關文章
- Go 語言處理 yaml 檔案GoYAML
- YAML檔案語法及示例YAML
- java解析yaml配置檔案JavaYAML
- YAML檔案YAML
- Spring YAML與屬性檔案配置檔案對比 | BaeldungSpringYAML
- kubernetes實戰篇之helm示例yaml檔案檔案詳細介紹YAML
- YAML檔案簡介YAML
- 【SpringBoot】YAML 配置檔案Spring BootYAML
- 入門Kubernetes - YAML檔案YAML
- c++ 解析yaml檔案C++YAML
- springboot專案中yaml檔案Spring BootYAML
- python讀取yaml配置檔案的方法PythonYAML
- Go 語言解析 yaml 檔案的方法GoYAML
- deployment.yaml 檔案解讀YAML
- python pyyaml操作yaml配置檔案PythonYAML
- .yaml引數檔案的編寫和使用YAML
- 實時流處理與分散式儲存過程中對檔案的操作分散式儲存過程
- 使用yaml檔案讀取資料YAML
- 使用Kubesec檢查YAML檔案安全YAML
- yaml的使用YAML
- 推薦一個yaml檔案轉json檔案的線上工具YAMLJSON
- Go讀取yaml檔案到struct類GoYAMLStruct
- SpringBoot2配置檔案application.yamlSpring BootAPPYAML
- 只需要一份Yaml檔案,即可實現UI自動化YAMLUI
- Spring創始人Rod大叔對YAML的真實想法SpringYAML
- Yaml檔案語法及讀寫小結YAML
- yaml檔案中在哪加名稱空間?YAML
- 用Java實現Stream流處理中的滑窗Java
- Spring Boot、Nacos配置檔案properties、yml、yaml的優先順序Spring BootYAML
- 修改SpringBoot的配置檔案application.yaml後啟動失敗Spring BootAPPYAML
- 使用資料流的思想處理檔案
- 使用java的MultipartFile實現layui官網檔案上傳實現全部示例,java檔案上傳JavaUI
- yaml配置YAML
- YAML 使用YAML
- spring boot 專案報錯找不到compose.yaml檔案Spring BootYAML
- CDC YAML 在阿里雲的最佳實踐YAML阿里
- k8s命令與k8s yaml資源描述檔案的基本使用K8SYAML
- 在K8S中,deployment的yaml檔案如何編寫呢?K8SYAML
- 處理 Linux 檔案的 3 個技巧Linux