对文件封装util

package com.tools.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class FileUtil {

    public static void writeObject2File(Object obj, String path) {
        File file = new File(path);
        writeObject2File(obj, file);
    }

    /**
     * 写 object
     * @param obj
     * @param file
     */
    public static void writeObject2File(Object obj, File file) {
        FileOutputStream out;
        try {
            out = new FileOutputStream(file);
            ObjectOutputStream objOut = new ObjectOutputStream(out);
            objOut.writeObject(obj);
            objOut.flush();
            objOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Object readObjectFromFile(String path) {
        File file = new File(path);
        return readObjectFromFile(file);
    }

    /**
     * 读取保存的object
     * @param file
     * @return
     */
    public static Object readObjectFromFile(File file) {
        Object temp = null;
        FileInputStream in;
        try {
            in = new FileInputStream(file);
            ObjectInputStream objIn = new ObjectInputStream(in);
            temp = objIn.readObject();
            objIn.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return temp;
    }

    /**
     * 文件copy
     * @param oldPath
     * @param newPath
     */
    public static void copy(String oldPath, String newPath) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        int byteread = 0;
        File oldfile = new File(oldPath);
        try {
            if (oldfile.exists()) {
                fis = new FileInputStream(oldPath);
                fos = new FileOutputStream(newPath);
                byte[] buffer = new byte[1024];
                while ((byteread = fis.read(buffer)) != -1) {
                    fos.write(buffer, 0, byteread);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
public static File getCacheDir(Context ctx) {
    File file = ctx.getExternalCacheDir();
    if (file == null) {
        file = ctx.getCacheDir();
    }
    return file;
}

public static File getImgFileDir(Context ctx) {
    File file = ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    if (file == null) {
        file = ctx.getFilesDir();
    }
    return file;
}

public static File getTempDir() {
    File file = Environment.getExternalStorageDirectory();
    File tempDir = new File(file.getAbsoluteFile() + "/iasku/temp/");
    if (!tempDir.exists()) {
        //可以创建当前的file  没有上级目录的 也可以创建
        tempDir.mkdirs();
    }

    return tempDir;
}

public static File getDownloadDir() {
    String path = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/iasku/downloads/";
    File file = new File(path);
    if (!file.exists()) {
        file.mkdirs();
    }
    return file;
}

/**
 * 音频文件保存路径
 * @return
 */
public static File getDownloadAudioDir() {
    String path = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/iasku/audio/";
    File file = new File(path);
    if (!file.exists()) {
        file.mkdirs();
    }
    return file;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值