android文件操作相关的工具

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,欢迎大家进入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值