package com.xjf.repository.utils;
import android.content.Context;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* IO utils
*
* @author Vladislav Bauer
*/
public class IOUtils {
private IOUtils() {
throw new AssertionError();
}
/**
* Close closable object and wrap {@link IOException} with {@link
* RuntimeException}
*
* @param closeable closeable object
*/
public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
}
}
}
/**
* Close closable and hide possible {@link IOException}
*
* @param closeable closeable object
*/
public static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
// Ignored
}
}
}
/**
* 保存文本
*
* @param fileName 文件名字
* @param content 内容
* @param append 是否累加
* @return 是否成功
*/
public static boolean saveTextValue(String fileName, String content, boolean append) {
try {
File textFile = new File(fileName);
if (!append && textFile.exists()) textFile.delete();
FileOutputStream os = new FileOutputStream(textFile);
os.write(content.getBytes("UTF-8"));
os.close();
} catch (Exception ee) {
return false;
}
return true;
}
/**
* 删除目录下所有文件
*
* @param Path 路径
*/
public static void deleteAllFile(String Path) {
// 删除目录下所有文件
File path = new File(Path);
File files[] = path.listFiles();
if (files != null) {
for (File tfi : files) {
if (tfi.isDirectory()) {
System.out.println(tfi.getName());
} else {
tfi.delete();
}
}
}
}
public static void deleteDbFile(Context context, String tab_name) {
InputStream in = null;
FileOutputStream out = null;
/**data/data/路径*/
String path = "/data/data/" + context.getPackageName() + "/databases";
File file = new File(path + "/" + tab_name);
if (file.exists()) {//删除已经存在的
file.deleteOnExit();
}
}
/**
* 将assets文件夹下/db的本地库拷贝到/data/data/下
*
* @param context
* @param tab_name
*/
public static void copyDbFile(Context context, String tab_name) {
InputStream in = null;
FileOutputStream out = null;
/**data/data/路径*/
String path = "/data/data/" + context.getPackageName() + "/databases";
File file = new File(path + "/" + tab_name);
try {
//创建文件夹
File file_ = new File(path);
if (!file_.exists()) {
file_.mkdirs();
}
if (file.exists())//删除已经存在的
{
return;//存在则不拷贝
// file.deleteOnExit();
}
//创建新的文件
if (!file.exists()) {
file.createNewFile();
}
in = context.getAssets().open(tab_name); // 从assets目录下复制
out = new FileOutputStream(file);
int length = -1;
byte[] buf = new byte[1024];
while ((length = in.read(buf)) != -1) {
out.write(buf, 0, length);
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) in.close();
if (out != null) out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}