-
首先需要引入fastjson的依赖文件,以下是maven的依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency>
-
然后是将json字符串写到文件中去:
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; public class JsonUtils { public static boolean createJsonFile(Object jsonData, String filePath) { String content = JSON.toJSONString(jsonData, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat); try { File file = new File(filePath); // 创建上级目录 if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } // 如果文件存在,则删除文件 if (file.exists()) { file.delete(); } // 创建文件 file.createNewFile(); // 写入文件 Writer write = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8); write.write(content); write.flush(); write.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } }
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240825082459112-376424429.jpg)