-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task2.java
60 lines (52 loc) · 1.59 KB
/
Task2.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
49
50
51
52
53
54
55
56
57
58
59
60
package org.example;
import java.io.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.List;
public class Task2 {
private List<User> users = new ArrayList<>();
public void execute(String path) throws IOException {
readFile(path);
convertToJson();
}
private void readFile(String path){
try(BufferedReader br = new BufferedReader(new FileReader(path))){
String line = br.readLine();
while((line=br.readLine())!=null){
String[] parts = line.split(" +");
users.add(new User(parts[0],Integer.parseInt(parts[1])));
}
}
catch (IOException ex){
System.out.println(ex.getMessage());
}
}
private void convertToJson() throws IOException {
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(users);
FileWriter fw = new FileWriter(new File(System.getProperty("user.dir")+"\\src\\main\\java\\org\\example\\user.json"));
fw.write(json);
fw.close();
}
private class User{
private String name;
private int age;
public User(String name,int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}