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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
| public class FSL {
public final int PLACE_INTERNAL_CASHE = 0;
public final int PLACE_INTERNAL_FILES = 1;
public final int PLACE_EXTERNAL_CASHE = 2;
public final int PLACE_EXTERNAL_FILES = 3;
public final int PLACE_EXTERNAL_STORAGE = 4;
//public final int PLACE_EXTERNAL_STORAGE_PUBLIC = 5;
public final String LOG_TAG = "FSL";
public FSL() {
}
public void FileSave(int place, String relativePath, String fileName, byte[] bytes) {
File path = GetPlacePath(place);
if (path == null) return;
path = new File(path.getAbsolutePath() + "/" + relativePath);
path.mkdir();
File file = new File(path, fileName);
try {
FileOutputStream fout = new FileOutputStream(file);
fout.write(bytes);
fout.close();
Log.d(LOG_TAG, "File save: " + file.getAbsolutePath());
} catch (IOException e) {
Log.e(LOG_TAG, e.getMessage());
} catch (Exception ex) {
Log.e(LOG_TAG, ex.getMessage());
}
}
public byte[] FileLoad(int place, String relativePath, String fileName) {
byte[] bytes = new byte[0];
File path = GetPlacePath(place);
if (path == null) return bytes;
path = new File(path.getAbsolutePath() + "/" + relativePath);
File file = new File(path, fileName);
try {
FileInputStream fin = new FileInputStream(file);
bytes = new byte[fin.available()];
fin.read(bytes);
fin.close();
Log.d(LOG_TAG, "File load: " + file.getAbsolutePath());
} catch (IOException e) {
Log.e(LOG_TAG, e.getMessage());
} catch (Exception ex) {
Log.e(LOG_TAG, ex.getMessage());
}
return bytes;
}
public void FileDelete(int place, String relativePath, String fileName) {
File path = GetPlacePath(place);
if (path == null) return;
path = new File(path.getAbsolutePath() + "/" + relativePath);
File file = new File(path, fileName);
try {
String str = file.getAbsolutePath();
file.delete();
Log.d(LOG_TAG, "File deleted: " + str);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
}
public File GetPlacePath(int place) {
File path = null;
if ((place == PLACE_EXTERNAL_CASHE)
|| (place == PLACE_EXTERNAL_FILES)
|| (place == PLACE_EXTERNAL_STORAGE)) {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Log.d(LOG_TAG, "SD-card not available: " + Environment.getExternalStorageState());
return path;
}
}
switch (place) {
case PLACE_INTERNAL_CASHE:
path = getCacheDir();
break;
case PLACE_INTERNAL_FILES:
path = getFilesDir();
break;
case PLACE_EXTERNAL_CASHE:
path = getExternalCacheDir();
break;
case PLACE_EXTERNAL_FILES:
path = getExternalFilesDir(null);
break;
case PLACE_EXTERNAL_STORAGE:
path = Environment.getExternalStorageDirectory();
break;
// case PLACE_EXTERNAL_STORAGE_PUBLIC:
// path = Environment.getExternalStoragePublicDirectory(null);
// break;
}
return path;
}
}
public void Test() {
String relativePath = "relativePath";
Calendar calendar = Calendar.getInstance();
String fileName = String.format("fileName_%tY%tm%td%tH%tM%tS",
calendar, calendar, calendar, calendar, calendar, calendar);
byte[] bytes = new byte[]{
(byte) (calendar.get(Calendar.YEAR) - 2000),
(byte) calendar.get(Calendar.MONTH),
(byte) calendar.get(Calendar.DAY_OF_MONTH),
(byte) calendar.get(Calendar.HOUR_OF_DAY),
(byte) calendar.get(Calendar.MINUTE),
(byte) calendar.get(Calendar.SECOND)
};
FSL fsl = new FSL();
Log.d(fsl.LOG_TAG, "Start FSL");
for (int i = 0; i < 5; i++) {
Log.d(fsl.LOG_TAG, "i=" + Integer.toString(i));
fsl.FileSave(i, relativePath, fileName, bytes);
if (bytes[5] != fsl.FileLoad(i, relativePath, fileName)[5])
Log.d(fsl.LOG_TAG, "ERROR");
fsl.FileDelete(i, relativePath, fileName);
}
} |