zuoxiao
2024-03-06 1cbb505922e010cace51020773a2dc1529713322
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
package com.dayu.rechargeqh.utils;
 
import android.content.Context;
import android.os.Environment;
import android.util.Log;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
/**
 * Created by zuoxiao on 2018/12/31.
 */
 
public class MyFileUtil {
    public static String SqlitePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "yanzhou" + File.separator + "data" + File.separator;
 
    public static String getSDPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState()
                .equals(android.os.Environment.MEDIA_MOUNTED);//判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();//获取跟目录
        }
        return sdDir.toString();
    }
 
    /**
     * @param context   context
     * @param assetName asset文件
     * @param savePath  目标路径
     * @param saveName  目标文件名
     */
    public static String copyFileFromAssets(Context context, String assetName, String savePath, String saveName) {
        // 若目标文件夹不存在,则创建
        String path = context.getFilesDir() + "/" + savePath;
        File dir = new File(path);
        if (!dir.exists()) {
            if (!dir.mkdir()) {
                Log.d("FileUtils", "mkdir error: " + savePath);
                return "";
            }
        }
 
        // 拷贝文件
        String filename = path + "/" + saveName;
        File file = new File(filename);
        if (!file.exists()) {
            try {
                InputStream inStream = context.getAssets().open(assetName);
                FileOutputStream fileOutputStream = new FileOutputStream(filename);
 
                int byteread;
                byte[] buffer = new byte[1024];
                while ((byteread = inStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, byteread);
                }
                fileOutputStream.flush();
                inStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Log.d("FileUtils", "[copyFileFromAssets] copy asset file: " + assetName + " to : " + filename);
        } else {
            Log.d("FileUtils", "[copyFileFromAssets] file is exist: " + filename);
        }
        return path;
    }
 
 
}