package com.dayu.recharge.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;
|
}
|
|
|
}
|