-
Notifications
You must be signed in to change notification settings - Fork 0
/
aArray.java
69 lines (57 loc) · 1.69 KB
/
aArray.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
package ceddy.ajds;
import android.content.Context;
import org.json.JSONArray;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
/**
* Created by Ceddy Muhoza.
*/
public class aArray {
private final String USER_FILE;
Context context;
public aArray(Context context, String database){
this.context = context;
USER_FILE = database;
}
public void writeUserFile(JSONArray me){
if(checkUserFile())
deleteUserFile();
FileOutputStream outputStream;
try {
outputStream = context.openFileOutput(USER_FILE, Context.MODE_PRIVATE);
outputStream.write(me.toString().getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public JSONArray readUserFile(){
File dir = context.getFilesDir();
File file = new File(dir, USER_FILE);
StringBuilder text = new StringBuilder();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
return new JSONArray(text.toString());
} catch (Exception e) {
return null;
}
}
public void deleteUserFile(){
File dir = context.getFilesDir();
File file = new File(dir, USER_FILE);
file.delete();
}
public boolean checkUserFile(){
File dir = context.getFilesDir();
File file = new File(dir, USER_FILE);
return file.exists();
}
}