-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYtfFileUtil.java
More file actions
498 lines (453 loc) · 15.2 KB
/
Copy pathYtfFileUtil.java
File metadata and controls
498 lines (453 loc) · 15.2 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package com.yuantuan.ytwebview.utils;
import android.content.Context;
import android.content.res.Resources;
import android.os.Environment;
import android.util.Log;
import com.yuantuan.ytwebview.file.Question;
import com.yuantuan.ytwebview.params.YtfConstant;
import com.yuantuan.ytwebview.safety.EncryptionHandle;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
/**
* =============================================================================
* [YTF] (C)2015-2099 Yuantuan Inc.
* Link http://www.ytframework.cn
* =============================================================================
*
* @author Like<[email protected]>
* @created 2016/7/7.
* @description 一些对数据存储,数据操作,SD卡操作有关的
* =============================================================================
*/
public class YtfFileUtil {
private static Context mContext;
/**
* 包名
*/
private static String packageName;
/**
* 获取资源
*/
private static Resources resources;
/**
* 单例
*/
private static YtfFileUtil ytfFileUtil;
private YtfFileUtil() {
}
public static void init(Context context) {
mContext = context;
packageName = mContext.getPackageName();
resources = mContext.getResources();
ytfFileUtil = new YtfFileUtil();
}
/**
* 单例模式
*
* @return
*/
public static YtfFileUtil getInstance() {
if (ytfFileUtil == null) {
throw new NullPointerException("请先初始化YtfFileUtil类");
} else {
return ytfFileUtil;
}
}
/**
* 判断SD卡是否正常挂载
*
* @return
*/
public static boolean SDCardIsWork() {
return Environment.getExternalStorageState().equals("mounted");
}
/**
* 获取内置SD卡的路径
*
* @return
*/
public static String getExternaStoragePath() {
return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
}
/**
* 删除文件
*
* @param file
*/
public static void delete(File file) {
if (file.isFile()) {
file.delete();
} else {
File[] list = file.listFiles();
if (list != null && list.length != 0) {
File[] file1 = list;
for (int i = 0; i < list.length; ++i) {
File item = file1[i];
delete(item);
}
file.delete();
} else {
file.delete();
}
}
}
/**
* 计算文件大小
*
* @param file
* @return
*/
public static long computeDirOrFileSize(File file) {
if (file != null && file.exists()) {
if (file.isFile()) {
return file.length();
} else {
long length = 0L;
File[] list = file.listFiles();
if (list != null) {
for (int i = 0; i < list.length; ++i) {
File item = list[i];
if (item.isFile()) {
length += item.length();
} else {
length += computeDirOrFileSize(item);
}
}
}
return length;
}
} else {
return 0L;
}
}
/**
* 获取原生下面文件的文件流
*
* @param path
* @return
*/
public String getAssetsFileStream(String path) {
try {
InputStreamReader inputReader = new InputStreamReader(resources.getAssets().open(path));
if (inputReader != null) {
BufferedReader bufReader = new BufferedReader(inputReader);
String line = "";
String result = "";
while ((line = bufReader.readLine()) != null)
result += line;
return result;
} else {
throw new NullPointerException("未找到指定文件");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取指定文件夹下面的文件内容
*
* @param strFilePath
* @return
*/
public String getFileString(String strFilePath) {
/**判断SD是否工作*/
if (!SDCardIsWork()) {
return "";
}
File file = new File(strFilePath);
BufferedReader reader;
String text = "";
try {
// FileReader f_reader = new FileReader(file);
// BufferedReader reader = new BufferedReader(f_reader);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fis);
in.mark(4);
byte[] first3bytes = new byte[3];
in.read(first3bytes);// 找到文档的前三个字节并自动判断文档类型。
in.reset();
L.d("格式:first3bytes[0]="+first3bytes[0]+",first3bytes[1]="+first3bytes[1]+",first3bytes[2]="+first3bytes[2]);
if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB
&& first3bytes[2] == (byte) 0xBF) {// utf-8
L.d("格式:utf-8");
reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFE) {
L.d("格式:unicode");
reader = new BufferedReader(
new InputStreamReader(in, "unicode"));
} else if (first3bytes[0] == (byte) 0xFE
&& first3bytes[1] == (byte) 0xFF) {
L.d("格式:utf-16be");
reader = new BufferedReader(new InputStreamReader(in,
"utf-16be"));
} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFF) {
L.d("格式:utf-16le");
reader = new BufferedReader(new InputStreamReader(in,
"utf-16le"));
} else if (first3bytes[0] ==123 &&first3bytes[1]==13&&first3bytes[2]==10) {
/**UTF-8无BOM*/
reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
} else {
L.d("格式:GBK");
reader = new BufferedReader(new InputStreamReader(in, "GBK"));
}
String str = reader.readLine();
int index = 0;
int line = 0;
List<Question> questions = new ArrayList<>();
while (str != null) {
while (line < 3) {
text = text + str + "\r\n";
str = reader.readLine();
line++;
}
line = 0;
Question question = new Question();
String[] num = text.split("\r\n");
for (int i = 0; i < num.length; i++) {
if (i % 3 == 0) {
question.setId(index);
}
if (i % 3 == 2) {
question.setQuestion(num[i]);
}
if (i % 3 == 1) {
question.setAnwer(num[i]);
}
}
questions.add(question);
index++;
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return text;
}
/**
* 读取SD卡中文本文件,无编码格式
*
* @param fileName
* @return
*/
public String readSDFile(String fileName) {
StringBuffer sb = new StringBuffer();
File file = new File(fileName);
try {
FileInputStream fis = new FileInputStream(file);
int c;
while ((c = fis.read()) != -1) {
sb.append((char) c);
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* 写数据到SD
*
* @param str 写入字符串
* @param pathName 路径,路径样式/sdcard/test/
* @param name 名称
*/
public static void writeFileToSD(String str, String pathName, String name) {
/**判断SD是否工作*/
if (!SDCardIsWork()) {
return;
}
try {
File path = new File(pathName);
File file = new File(pathName + name);
if (!path.exists()) {
//L.d("TestFile", "Create the path:" + pathName);
path.mkdir();
}
if (!file.exists()) {
//L.d("TestFile", "Create the file:" + name);
file.createNewFile();
}
FileOutputStream stream = new FileOutputStream(file);
byte[] buf = str.getBytes();
stream.write(buf);
stream.close();
} catch (Exception e) {
Log.e("TestFile", "Error on writeFilToSD.");
e.printStackTrace();
}
}
/**
* 从SD卡读取文件
*
* @param path 文件路径+名称,路径样式/sdcard/test/
* @return
*/
public static String readFileToSD(String path) {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File targetFile = new File(path);
String readedStr = "";
try {
if (!targetFile.exists()) {
targetFile.createNewFile();
return "No File error ";
} else {
InputStream in = new BufferedInputStream(new FileInputStream(targetFile));
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String tmp;
while ((tmp = br.readLine()) != null) {
readedStr += tmp;
}
br.close();
in.close();
return readedStr;
}
} catch (Exception e) {
return e.toString();
}
} else {
return "SD Card error";
}
}
/**
* 复制assets目录下指定所有文件到指定目录下面
*
* @param assetDir
* @param dir
*/
public static void copyAssets(String assetDir, String dir) {
String[] files;
try {
// 获得Assets一共有几多文件
files = resources.getAssets().list(assetDir);
//L.d("<>_<>", "copyAssets: "+files.length);
} catch (IOException e1) {
return;
}
File mWorkingPath = new File(dir);
// 如果文件路径不存在
if (!mWorkingPath.exists()) {
// 创建文件夹
if (!mWorkingPath.mkdirs()) {
// 文件夹创建不成功时调用
}
}
for (int i = 0; i < files.length; i++) {
try {
// 获得每个文件的名字
String fileName = files[i];
// 根据路径判断是文件夹还是文件
if (!fileName.contains(".")) {
if (0 == assetDir.length()) {
copyAssets(fileName, dir + fileName + "/");
} else {
copyAssets(assetDir + "/" + fileName, dir + "/"
+ fileName + "/");
}
continue;
}
File outFile = new File(mWorkingPath, fileName);
if (outFile.exists())
outFile.delete();
InputStream in = null;
if (0 != assetDir.length())
in = mContext.getAssets().open(assetDir + "/" + fileName);
else
in = mContext.getAssets().open(fileName);
OutputStream out = new FileOutputStream(outFile);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 从assets目录中复制整个文件夹内容
*
* @param context Context 使用CopyFiles类的Activity
* @param oldPath String 原文件路径 如:/aa
* @param newPath String 复制后路径 如:xx:/bb/cc
*/
public static void copyFilesFassets(Context context, String oldPath, String newPath) {
try {
String fileNames[] = context.getAssets().list(oldPath);//获取assets目录下的所有文件及目录名
//L.d("<>_<>", "copyAssets: "+fileNames.length);
if (fileNames.length > 0) {//如果是目录
File file = new File(newPath);
file.mkdirs();//如果文件夹不存在,则递归
for (String fileName : fileNames) {
copyFilesFassets(context, oldPath + "/" + fileName, newPath + "/" + fileName);
}
} else {//如果是文件
InputStream is = context.getAssets().open(oldPath);
FileOutputStream fos = new FileOutputStream(new File(newPath));
byte[] buffer = new byte[1024];
int byteCount = 0;
while ((byteCount = is.read(buffer)) != -1) {//循环从输入流读取 buffer字节
fos.write(buffer, 0, byteCount);//将读取的输入流写入到输出流
}
fos.flush();//刷新缓冲区
is.close();
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
//如果捕捉到错误则通知UI线程
}
}
/**
* 获取包名
*
* @return
*/
public String getPackageName() {
return mContext.getPackageName();
}
/**
* 获取热修复路径
*
* @return
*/
public String getRepair() {
return YtfConstant.DATA_HEAD + getPackageName() + "/";
}
/**
* 获取Mime类型
*
* @param str
* @return
*/
public String getFileMimeType(String str) {
FileNameMap fileNameMap = URLConnection.getFileNameMap();
return fileNameMap.getContentTypeFor(str);
}
}