本地文件操作类

public class LocaHostUtils {

    //获取当前的时间
    public static String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());


    //这个路径是 当前路径/OPE/
    public static String savepath = "/sdcard/a/";

    public static String logpath = "/sdcard/b";

    public static String imagePath = "/sdcard/c";

    public static String imageDtPath = "/sdcard/d";

    public static File fileName;

    public static void saveLogData(String strcontent, String bundName) {

        //这个路径是 信息
        String fileNamee = ".txt";

        File file1 = new File(logpath);
        if (!file1.exists()) {
            file1.mkdirs();  //创建文夹
        }

        File file2 = new File(file1, bundName + fileNamee);
        if (!file2.exists()) {
            try {
                file2.createNewFile();  //创建文件
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //一行一行的读
        FileWriter fw = null;
        BufferedWriter bw = null;   //
        String datetime = "";
        try {
            fw = new FileWriter(file2, true);   //对这个进行扫描
            bw = new BufferedWriter(fw);   //缓冲区对文字的写入

            String myreadline = datetime + "" + strcontent;
            bw.write(myreadline + "\n");       //往进写的东西
            bw.flush(); //刷新
            bw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        fileName = file2;

    }

    //创建文件夹
    public static Boolean creatFolder(String foldername) {
        Boolean f = false;
        File file1 = new File(foldername);
        if (!file1.exists()) {
            f = file1.mkdirs();  //创建文夹
            return f;
        }
        return f;
    }

    //通过路径找到这个文件的名字
    public static String getFileName(String pathandname) {

        int start = pathandname.lastIndexOf("/");
        int end = pathandname.lastIndexOf(".");
        if (start != -1 && end != -1) {
            return pathandname.substring(start + 1, end);
        } else {
            return null;
        }

    }

    //这个是将数据库里面的路径寻找到
    public static void getJson(List<String> path) {
        File file = new File(savepath + timeStamp);
        if (!file.exists()) {
            file.mkdirs();
        }
        for (int i = 0; i < path.size(); i++) {
            char c = (char) (Math.random() * 26 + 97);    //随机数
            copyFile(path.get(i), file + File.separator + c + ".jpeg");

        }
    }

    /**
     * 复制单个文件
     * @param oldPath String 原文件路径 如:c:/fqf.txt
     * @param newPath String 复制后路径 如:f:/fqf.txt
     */
    public static void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            //Log.e("copyFile1", "oldPath:" + oldPath + "---------->newPath:" + newPath);
            File oldfile = new File(oldPath);
            if (oldfile.exists()) { //文件存在时
                InputStream inStream = new FileInputStream(oldPath); //读入原文件
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1024 * 5];
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小
                    fs.write(buffer, 0, byteread);
                }
                fs.flush();//刷新流
                fs.close();//关闭
                inStream.close();//关闭输入流
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //将bitmap 类型的图片保存到本地
    public static String saceBitmap(Bitmap bitmap, String name, String projectname, String mfloorname) {
        File file = new File(imagePath);
        if (!file.exists()) {      //如果不存在的话、就让他进行创建文件
            file.mkdir();     //创建文件
        }
        File file1 = new File(file + File.separator, timeStamp + "_" + name + ".jpeg");

        try {
            OutputStream os = new FileOutputStream(file1);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
            os.close();
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("----", e.toString());
            return null;
        }
        return file1.getAbsolutePath();
    }


    //将bitmap 类型的图片保存到本地
    public static String saceDtBitmap(Bitmap bitmap, String name) {
        File file = new File(imageDtPath);
        if (!file.exists()) {      //如果不存在的话、就让他进行创建文件
            file.mkdir();     //创建文件
        }
        File file1 = new File(file + File.separator, timeStamp + "_" + name + ".jpeg");

        try {
            OutputStream os = new FileOutputStream(file1);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
            os.close();
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("----", e.toString());
            return null;
        }
        return file1.getAbsolutePath();
    }

    //删除本地文件
    public static boolean deleteFile(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            return false;
        }
        if (!file.isFile()) {
            return false;
        }
        return file.delete();
    }

    //判断文件夹是不是空的
    public static boolean folderisBlank(String foldername) {
        File file = new File(foldername);  // 当前目录下的 testdir目录
        if (file.isDirectory()) {
            if (file.list().length > 0) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
//创建方法,删除文件夹中的所有文件包括文件夹本身
public void DeleteFile(String file)
{
    //去除文件夹和子文件的只读属性
    //去除文件夹的只读属性
    System.IO.DirectoryInfo fileInfo = new DirectoryInfo(file);
    fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
    //去除文件的只读属性
    System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);
    //判断文件夹是否还存在
    if (ContactsContract.Directory.Exists(file))
    {
        foreach (string f in Directory.GetFileSystemEntries(file))
        {
            if (File.Exists(f))
            {
                //如果有子文件删除文件
                File.Delete(f);
            }
            else
            {
                //循环递归删除子文件夹 
                DeleteFile(f);
            }
        }
        //删除空文件夹 
        Directory.Delete(file);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值