java - 如何使用GSon进行配置?阅读器出错,还有其他方式制作cfg吗?

标签 java json gson config

我尝试使用 Gson 进行配置我制作了以下代码示例:

    Channel c = new Channel(3, "TEST STRING!");
    Gson gson = new GsonBuilder().create();

    FileWriter writer = new FileWriter("config.json");

    gson.toJson(c, writer);

    writer.flush();

    Reader reader = new InputStreamReader(Bot.class.getResourceAsStream("config.json"), "UTF - 8");

    Gson ab = new GsonBuilder().create();

    Channel a = ab.fromJson(reader, Channel.class);

    System.out.println(a);

但我有错误:java.lang.NullPointerException 排队

Reader reader = new InputStreamReader(Bot.class.getResourceAsStream("config.json"), "UTF - 8");

我哪里出错了?

另一个问题,如何在文件中进行配置?

最佳答案

选项一:您正在读取的文件无法在运行时创建,您需要在开发环境中手动创建它,就像创建 java 源文件一样。这样它就可以位于类路径上,您可以通过 getResourceAsStream() 获取:

项目布局:

MyProject
└── src
    └── config.json // option one
    └── com
        └── myproject
            └── Main.java
            └── config.json // option two

您的代码:

// If you placed config.json at location 1 (notice the leading slash)
Reader reader = new InputStreamReader(Bot.class.getResourceAsStream("/config.json"), "UTF - 8");
// If you placed config.json at location 2 (notice no leading slash)
Reader reader = new InputStreamReader(Bot.class.getResourceAsStream("config.json"), "UTF - 8");

Gson ab = new GsonBuilder().create();

Channel a = ab.fromJson(reader, Channel.class);

System.out.println(a);
<小时/>

选项二:这可能是您问题的解决方案,但我想我应该澄清 getResourceAsStream() 的作用。不要尝试在类路径上查找 config.json,而是从刚刚保存到的文件中再次加载它。

// You might need to add this import
import java.nio.charset.StandardCharsets;

/*
 * THIS BLOCK SAVES THE `Channel` INSTANCE TO THE FILE `config.json`
 */
// I also fixed this. Always specify your encodings!
try(FileOutputStream fos = new FileOutputStream("config.json");
    OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
    BufferedWriter writer = new BufferedWriter(osw))
{
    Channel c = new Channel(3, "TEST STRING!");
    Gson gson = new GsonBuilder().create();
    gson.toJson(c, writer);
} // This is a "try-with-resources" statement. The Closeable resource defined in the try() block will automatically be closed at the end of the try block.

/*
 * THIS BLOCK RECONSTRUCTS THE JUST SAVED `Channel` instance from the file `config.json`
 */
// This opens the file 'config.json' in the working directory of the running program
try(FileInputStream fis = new FileInputStream("config.json");
    InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
    BufferedReader reader = new BufferedReader(isr))
{
    Gson ab = new GsonBuilder().create();
    Channel a = ab.fromJson(reader, Channel.class);
    System.out.println(a);
} // Again, a try-with-resources statement

关于java - 如何使用GSon进行配置?阅读器出错,还有其他方式制作cfg吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34482192/

相关文章:

java - 返回整数数组的 SQLite 查询数据

jquery - 将更多对象数组添加到本地存储键中

php - PHP 生成的 JSON 文件具有 application/octet-stream mime 类型

javascript - 递归的麻烦;解析 JSON

java - GSON 库忽略值字段中的 + 符号

java - 从外部类引用静态嵌套类对象

java - 运行框架时 JButton 丢失

java - 您可以使用 GSON 的 JsonElement 对大多数但不是所有类成员执行 equals 比较吗

java - 在scrollPane中显示多个图像会导致组件根据图像大小丢失

java - java 中 Class<T[]> 的实例,以便在 json 解析器中使用