package com.sun.framework.Utils;
import android.content.Context;
import com.sun.framework.CustomizeVC.LaunchActivity;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class FileUtils {
private Context context;
public String filePath;
public FileUtils(Context context){
this.context = context;
filePath = context.getFilesDir().getAbsolutePath();
filePath = filePath.replace("files","appCache"); // 路径:data/data/packageName/appCache/***
}
public boolean saveFileContent(Object obj, String fileName) {
String Path = filePath+"/"+fileName;
try {
File file = new File(Path);
if (!file.exists()) {
File dir = new File(file.getParent());
dir.mkdirs();
file.createNewFile();
}
FileOutputStream outStream = new FileOutputStream(file);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outStream);
objectOutputStream.writeObject(obj);
outStream.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public Object getFileObjectContent(String fileName) {
String Path = filePath+"/"+fileName;
Object obj = null;
try {
File file = new File(Path);
if (file.exists()) {
FileInputStream inStram = new FileInputStream(file);
ObjectInputStream objectInputStream = new ObjectInputStream(inStram);
obj = objectInputStream.readObject();
inStram.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public void saveFileContent(String str, String fileName) {
String Path = filePath+"/"+fileName;
try {
File file = new File(Path);
if (!file.exists()) {
File dir = new File(file.getParent());
dir.mkdirs();
file.createNewFile();
}
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(str.getBytes());
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public String getFileStringContent(String fileName) {
String Path = filePath+"/"+fileName;
String str = null;
try {
File file = new File(Path);
if (file.exists()) {
FileInputStream inStram = new FileInputStream(file);
int length = inStram.available();
byte[] buffer = new byte[length];
inStram.read(buffer);
str = new String(buffer, "UTF-8");
inStram.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
public List<String> getAllFileName() {
List<String> list = new ArrayList<>();
File fileTest = new File(filePath);
File[] files = fileTest.listFiles();
for (int i=0;i<files.length;i++){
File file = files[i];
String name = file.getName();
list.add(name);
}
return list;
}
public boolean deleteFile(String fileName) {
String Path = filePath+"/"+fileName;
File file = new File(Path);
if (file.isFile() && file.exists()) {
return file.delete();
}
return false;
}
/**************Assets****************/
public String getAssetsFileStringContent(String fileName) {
String str = null;
try {
InputStream inStram =context.getClass().getClassLoader().getResourceAsStream("assets/"+fileName);
int length = inStram.available();
byte[] buffer = new byte[length];
inStram.read(buffer);
str = new String(buffer, "UTF-8");
inStram.close();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
public static void copyDBToMemoryCard(Context context)
{
String customizeDbName = LaunchActivity.getInstance().customizeDbName();
String path="/data/data/"+ context.getPackageName()+"/databases"+"/"+ customizeDbName;
File file=new File(path);
if(!file.exists()){
try {
File dir = new File(file.getParent());
dir.mkdirs();
file.createNewFile();
OutputStream os=new FileOutputStream(path);
InputStream is = context.getResources().getAssets().open(customizeDbName);
//InputStream is =context.getClass().getClassLoader().getResourceAsStream("assets/"+customizeDbName);
int count;
byte[] buffer=new byte[1024];
while((count=is.read(buffer))>0)
os.write(buffer,0,count);
os.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
我的业余技术微信公众号:YKJGZH,欢迎大家进入