我需要使用ArrayList
创建一个.json.gz文件void func(ArrayList<AirConfig> configList)
{
String filePath = "abc.json.gz";
File file = new File(filePath);
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(file, configList);
}
然而,这确实有效,因为当我尝试使用以下命令解压缩文件时
gzip -cd configMasterAirport_Test.json.gz > configMasterAirport_Test.json
我收到以下错误,
abc.json.gz: not in gzip format
如何重写代码以便生成压缩的json,但我仍然可以使用Object Mapper
答案 0 :(得分:1)
您只需要进行一些小改动。
void func(ArrayList<AirConfig> configList)
{
FileOutputStream fStream = null;
GZIPOutputStream zStream = null;
try
{
String filePath = "abc.json.gz";
fStream = new FileOutputStream(filePath);
zStream = new GZIPOutputStream(new BufferedOutputStream(fStream));
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(zStream, configList);
}
finally
{
if (zStream != null)
{
zStream.flush();
zStream.close();
}
if (fStream != null)
{
fStream.flush();
fStream.close();
}
}
}