-
Notifications
You must be signed in to change notification settings - Fork 0
/
aObject.java
68 lines (57 loc) · 1.67 KB
/
aObject.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.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
/**
* Created by Ceddy Muhoza
*/
public class aObject {
Context context;
String USER_FILE;
public aObject(Context context, String u){
this.context = context;
this.USER_FILE = u;
}
public void writeUserFile(JSONObject 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 JSONObject 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 JSONObject(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();
}
}