• android java读写yaml文件


    目录

    申请读写权限:

    build.gradle中添加库引用:

    android java读写yaml文件

    java修改yaml文件 YamlFile:

    修改yaml文件方法2 Yaml:

    删除值:


    申请读写权限:

        android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
       

    build.gradle中添加库引用:

    1. dependencies {
    2. /** 日志库 **/
    3. implementation 'androidx.core:core-ktx:1.2.0'
    4. implementation 'androidx.appcompat:appcompat:1.1.0'
    5. // 集成日志库
    6. api 'com.jakewharton.timber:timber:5.0.1'
    7. implementation 'org.yaml:snakeyaml:1.33'
    8. api 'com.google.code.gson:gson:2.8.5'
    9. api 'com.squareup.okhttp3:okhttp:4.9.1'
    10. implementation 'com.alibaba:fastjson:2.0.28'
    11. }

    android java读写yaml文件

    1. import org.simpleyaml.configuration.file.YamlFile;
    2. import org.yaml.snakeyaml.DumperOptions;
    3. import org.yaml.snakeyaml.Yaml;
    4. private static void run() {
    5. File file = new File("/data/data/com.xx/files/config.yaml");
    6. if (file.exists()) {
    7. YamlFile yamlFile = new YamlFile(file);
    8. try {
    9. yamlFile.load();
    10. int debug = yamlFile.getInt("debug", 1);
    11. // Timber.i("debug: " + debug);
    12. Timber.i("debug: %s", debug);
    13. } catch (IOException e) {
    14. }
    15. }
    16. Map data = new HashMap<>();
    17. data.put("name", "John Doe");
    18. data.put("age", 30);
    19. DumperOptions options = new DumperOptions();
    20. options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    21. Yaml yaml = new Yaml(options);
    22. try (FileWriter writer = new FileWriter("path_to_your_file.yaml")) {
    23. yaml.dump(data, writer);
    24. } catch (IOException e) {
    25. e.printStackTrace();
    26. }
    27. }

    写文件可以参考下面的:java修改yaml文件 YamlFile 

    java修改yaml文件 YamlFile:

    1. private void modfiy_param(String key ,int value) {
    2. File file = new File("/data/data/com.xx/files/config.yaml");
    3. if (file.exists()) {
    4. YamlFile yamlFile = new YamlFile(file);
    5. try {
    6. yamlFile.load();
    7. int track_delay = yamlFile.getInt("aaa", 1);
    8. Timber.i("aaa: %s", aaa);
    9. yamlFile.set(key, value);
    10. yamlFile.save();
    11. } catch (IOException e) {
    12. e.printStackTrace();
    13. }
    14. }
    15. }

    修改yaml文件方法2 Yaml:

    1. Yaml yaml = new Yaml();
    2. Map data = null;
    3. try {
    4. data = yaml.load(new FileInputStream("/data/data/com.xx/files/config.yaml"));
    5. // 2. 修改这个对象
    6. data.put("name", "Jane Doe");
    7. // Map data = new HashMap<>();
    8. data.put(key, value);
    9. FileWriter writer = new FileWriter("/data/data/com.xx/files/config.yaml");
    10. yaml.dump(data, writer);
    11. } catch (FileNotFoundException e) {
    12. throw new RuntimeException(e);
    13. } catch (IOException e) {
    14. throw new RuntimeException(e);
    15. }

    删除值:

    1. import org.yaml.snakeyaml.Yaml;
    2. import java.io.FileInputStream;
    3. import java.io.FileWriter;
    4. import java.io.IOException;
    5. import java.util.Map;
    6. public class Main {
    7. public static void main(String[] args) {
    8. deleteFromYamlFile("path_to_your_file.yaml", "name");
    9. }
    10. public static void deleteFromYamlFile(String filePath, String key) {
    11. Yaml yaml = new Yaml();
    12. try {
    13. // 1. 读取文件并将其解析为一个对象
    14. Map data = yaml.load(new FileInputStream(filePath));
    15. // 2. 从这个对象中删除一个键
    16. data.remove(key);
    17. // 3. 将修改后的对象写回到文件
    18. FileWriter writer = new FileWriter(filePath);
    19. yaml.dump(data, writer);
    20. writer.close();
    21. } catch (IOException e) {
    22. e.printStackTrace();
    23. }
    24. }
    25. }

  • 相关阅读:
    c#简易学生管理系统
    商业智能中的决策, 数据和数据处理方法
    16 一元线性回归
    【Python】小知识:使用for循环来打印列表中的参数
    Django基础学习
    怎样定制开发小程序微商城_流程_报价_OctShop
    代理模式-静态动态代理-jdk动态代理-cglib动态代理
    ffmpeg播放时刻与视频文件时间戳对齐(同步)
    【算法|虚拟头节点|链表】移除链表元素
    带大量数据和公式的excel表格,手机打开数据正常,电脑打开数据就消失了
  • 原文地址:https://blog.csdn.net/jacke121/article/details/132784291