-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathGsonWriteList.java
48 lines (35 loc) · 1.25 KB
/
GsonWriteList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.zetcode;
import com.google.gson.Gson;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
class Item {
private final String name;
private final int quantity;
public Item(String name, int quantity) {
this.name = name;
this.quantity = quantity;
}
}
public class GsonWriteList {
public static void main(String[] args) throws IOException {
String fileName = "src/main/resources/items.json";
try (FileOutputStream fos = new FileOutputStream(fileName);
OutputStreamWriter isr = new OutputStreamWriter(fos,
StandardCharsets.UTF_8)) {
Gson gson = new Gson();
Item item1 = new Item("chair", 4);
Item item2 = new Item("book", 5);
Item item3 = new Item("pencil", 1);
List<Item> items = new ArrayList<>();
items.add(item1);
items.add(item2);
items.add(item3);
gson.toJson(items, isr);
}
System.out.println("Items written to file");
}
}