本地文件操作類

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);
    }
}
發佈了23 篇原創文章 · 獲贊 9 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章