-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFileControl.java
85 lines (67 loc) · 2.12 KB
/
FileControl.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.google.gson.Gson;
public class FileControl extends Query {
private String tname = super.tName;
public static JSONArray readFromFile(String dname, String tname){
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader(tname));// change to exact location of the file just written a prototype
JSONObject jsonObject = (JSONObject) obj;
//String data = jsonObject.get(tname).toString();
return (JSONArray)jsonObject.get(tname);
// the data is stored like this
/*
tname:
{
[
{record1},{record2}, etc.........
]
}
*/
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static void writeToFile(String dname,String tname,JSONObject data,boolean isAppend){
BufferedWriter bw=null;
try {
File file = new File(tname);
// Writing to a file
// got to dname directory
FileWriter writer = new FileWriter(file,isAppend);//name of the file
bw = new BufferedWriter(writer);
bw.write(data.toJSONString());
bw.flush();
//bw.close();
}
catch (IOException e) {
e.printStackTrace();
}
finally { // always close the file
if (bw != null) try {
bw.close();
} catch (IOException ioe2) {
// just ignore it
}
}
}
}