将 json 个对象列表压缩到 JAVA 中的 gzip 文件
compress list of json obects to gzip file in JAVA
我想使用 JAVA
将 JSON 个对象的列表转换为 gzip 文件
下面是我的JSON文件
[
{
"id": "01",
"Status": "Open",
"siteId": "01",
"siteName": "M1"
},
{
"id": "02",
"Status": "Open",
"siteId": "02",
"siteName": "M2"
},
{
"id": "03",
"Status": "Open",
"siteId": "03",
"siteName": "M3"
}
]
到目前为止编写的代码:
public static void main(String args[]) throws IOException
{
final ObjectMapper objectMapper = new ObjectMapper();
String fileName = "jsonList.json";
ClassLoader classLoader = className.class.getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
System.out.println("File Found : " + file.exists());
List<document> list = objectMapper.readValue(file,new TypeReference<List<document>>(){});
System.out.println("List of json objects");
//code to compress list of json objects (list)
}
文档class
public class document {
public String id;
public String status;
public String sideId;
public String siteName;
}
请建议我压缩列表的代码
谢谢!
你可以使用这样的东西,StudentL:
OutputStream outputStream = new FileOutputStream("output_file.zip");
GZIPOutputStream gzipOuputStream = new GZIPOutputStream(outputStream);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOuputStream);
for (Document doc : list) {
objectOutputStream.writeObject(doc);
}
objectOutputStream.close();
我想这是为了练习,否则认为你这样做没有多大意义。您不需要阅读 JSON 并执行整个解组操作;您可以简单地获取现有文件并将其压缩。
奖金编辑:您的代码中有两件事往往会激怒 Java 开发人员。首先是Java中类的名字通常写在Camel case中。第二个是在花括号前不要使用换行符,就像在 C/C++ 和其他语言中那样。
错误:
public class Whatever
{
右:
public class Whatever {
回头见!
以下是我认为您可以做的事情:
List<Map<String, String>> jsonList = ...;
// Create a FileOutputStream
FileOutputStream fileOutputStream
= new FileOutputStream("/path/to/file");
// Wrap the FileOutputStream in a BufferedOutputStream
// so that we write in larger chunks for performance
BufferedOutputStream bufferedOutputStream
= new BufferedOutputStream(fileOutputStream);
// Wrap the BufferedOutputStream in a GIZPOutputStream
// so that when we write to the gzipOutputStream,
// we first compress then buffer then write to the file.
GZIPOutputStream compressedOutputStream
= new GZIPOutputStream(bufferedOutputStream);
// Write the JSON map into the compressedOutputStream
objectMapper.writeValue(compressedOutputStream, jsonList);
这是使用对象映射器和 OutputStreams 将对象压缩到文件的 Jackson Library you can refer to; And here is a detailed explanation。
我想使用 JAVA
将 JSON 个对象的列表转换为 gzip 文件下面是我的JSON文件
[
{
"id": "01",
"Status": "Open",
"siteId": "01",
"siteName": "M1"
},
{
"id": "02",
"Status": "Open",
"siteId": "02",
"siteName": "M2"
},
{
"id": "03",
"Status": "Open",
"siteId": "03",
"siteName": "M3"
}
]
到目前为止编写的代码:
public static void main(String args[]) throws IOException
{
final ObjectMapper objectMapper = new ObjectMapper();
String fileName = "jsonList.json";
ClassLoader classLoader = className.class.getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
System.out.println("File Found : " + file.exists());
List<document> list = objectMapper.readValue(file,new TypeReference<List<document>>(){});
System.out.println("List of json objects");
//code to compress list of json objects (list)
}
文档class
public class document {
public String id;
public String status;
public String sideId;
public String siteName;
}
请建议我压缩列表的代码 谢谢!
你可以使用这样的东西,StudentL:
OutputStream outputStream = new FileOutputStream("output_file.zip");
GZIPOutputStream gzipOuputStream = new GZIPOutputStream(outputStream);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOuputStream);
for (Document doc : list) {
objectOutputStream.writeObject(doc);
}
objectOutputStream.close();
我想这是为了练习,否则认为你这样做没有多大意义。您不需要阅读 JSON 并执行整个解组操作;您可以简单地获取现有文件并将其压缩。
奖金编辑:您的代码中有两件事往往会激怒 Java 开发人员。首先是Java中类的名字通常写在Camel case中。第二个是在花括号前不要使用换行符,就像在 C/C++ 和其他语言中那样。
错误:
public class Whatever
{
右:
public class Whatever {
回头见!
以下是我认为您可以做的事情:
List<Map<String, String>> jsonList = ...;
// Create a FileOutputStream
FileOutputStream fileOutputStream
= new FileOutputStream("/path/to/file");
// Wrap the FileOutputStream in a BufferedOutputStream
// so that we write in larger chunks for performance
BufferedOutputStream bufferedOutputStream
= new BufferedOutputStream(fileOutputStream);
// Wrap the BufferedOutputStream in a GIZPOutputStream
// so that when we write to the gzipOutputStream,
// we first compress then buffer then write to the file.
GZIPOutputStream compressedOutputStream
= new GZIPOutputStream(bufferedOutputStream);
// Write the JSON map into the compressedOutputStream
objectMapper.writeValue(compressedOutputStream, jsonList);
这是使用对象映射器和 OutputStreams 将对象压缩到文件的 Jackson Library you can refer to; And here is a detailed explanation。