2018年8月14日 星期二

建立文件夾,文件寫入,內存,SD卡剩餘空間

        // ---先檢查權限-6.0 --------------------------
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_READ_PHONE_STATE);
        }


//檢查SD
    public static boolean isSDCardEnable(){
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }

            //获取内存可用剩余空间
            long romFreeSpace=Environment.getDataDirectory().getFreeSpace();
            //获取SD卡可用剩余空间
            long SDFreeSpace= Environment.getExternalStorageDirectory().getFreeSpace();
            String formatter= android.text.format.Formatter.formatFileSize(this,romFreeSpace);
            String SDformatter= android.text.format.Formatter.formatFileSize(this,SDFreeSpace);
            Log.e("Debug","內存可用"+ formatter);
            Log.e("Debug","sd卡可用"+ SDformatter);



 String directoryPath= Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"File_Text_Demo"+File.separator;  // 路徑  路徑資料夾下
 String FileName= directoryPath+getNowTime()+".txt";  // 路徑 + 檔案名稱
    //建立文件夾 及 檔案
    public void CreatText(){
        File file=new File(directoryPath);
        if(!file.exists()){
            try {
               //按照指定的路径创建文件夹
                file.mkdirs();
            }catch (Exception e){

            }
        }
         File dir= new File(FileName);
        if(!dir.exists()){
            try{
                dir.createNewFile(); //在指定的文件中創建文件
                Log.e("Debug"," 創建成功");

            }catch (Exception e){
            }
        }
    }

//文件內容
 public void print( String str){
        FileWriter fw=null;
        BufferedWriter bw=null;
        String datetime="";
        try{
            CreatText();
            SimpleDateFormat tempDate=new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
            datetime= tempDate.format(new Date().toString());   //轉為字串
            fw= new FileWriter(FileName,true);
            // 创建FileWriter对象,用来写入字符流
       /**
                     * 如果想要每次写入,清除之前的内容,使用FileOutputStream流
                     */
          bw=new BufferedWriter(fw);// 将缓冲对文件的输出
          String myreadline= datetime+","+str;
          bw.write(myreadline + "\n"); //寫入文件
            bw.newLine();
            bw.flush();
            bw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
            try {
                bw.close();
                fw.close();
            } catch (IOException e1) {
            }
        }
    }


//當前時間
    public static String getNowTime(){
        //  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }

沒有留言:

張貼留言