Java将将字符串转化为Json然后写入文本

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
使用到的语句:
FileUtils.writeStringtoFile(File file, String data, String encoding, boolean append);
说明:其中file就是新建的file文件,data是要写入文件的内容,encoding是编码类型,不写是默认,append为true表示在文本内容后面添加内容而不会覆盖文件中的内容重写,不写则覆盖。
String code = “json”;
//字符串转json
JSONObject result = new JSONObject();
result.put(“userId”, code);
String writeCode = result.toJSONString();
//将json写入文件
String path = “path”;
File file = new File(path );
FileUtils.writeStringToFile(file, writeCode, “utf-8”);
结果:
在这里插入图片描述

你可能感兴趣的:(Java,java)