java拷貝檔案夾和android設定檔案許可權

來源:互聯網
上載者:User

拷貝檔案夾的代碼:

 public boolean copy(String file1,String file2) {
                   File in=new File(file1);
                   File out=new File(file2);
                   if(!in.exists()){
                           return false;
                   }
                   else {
                   }
                   if(!out.exists())
                           out.mkdirs();
                   File[] file=in.listFiles();
                   FileInputStream fin=null;
                   FileOutputStream fout=null;
                   for(int i=0;i<file.length;i++){
                   if(file[i].isFile()){
                           try {
                                   fin=new FileInputStream(file[i]);
                           } catch (FileNotFoundException e) {
                                   // TODO Auto-generated catch block
                                   e.printStackTrace();
                           }

                           try {
                                   File f = new File(file2+"/"+file[i].getName());
                                   fout=new FileOutputStream(f);
                           } catch (FileNotFoundException e) {
                                   // TODO Auto-generated catch block
                                   e.printStackTrace();
                           }

                           int c;
                           byte[] b=new byte[1024*5];
                           try {
                                   while((c=fin.read(b))!=-1){
                                           fout.write(b, 0, c);
                                   }
                                   fin.close();
                                   fout.flush();
                                   fout.close();
                           } catch (IOException e) {
                                   // TODO Auto-generated catch block
                                   e.printStackTrace();
                           }
   //              return true;
                   }
                   else copy(file1+"/"+file[i].getName(),file2+"/"+file[i].getName());
                   }

                   return false;

}

 

android上設定檔案夾許可權有兩種方法

1:

  try {
                    String command = "chmod 644 " + f.getAbsolutePath();
                    Runtime runtime = Runtime.getRuntime();
                    java.lang.Process proc = runtime.exec(command);
                    } catch (IOException e) {
                       e.printStackTrace();
                    }

2:

使用FileUtils,這個類預設是隱藏的,官方sdk中不包含這個類,所以代碼如果要使用這個類,需要將工程放到android源碼中編譯
          FileUtils.setPermissions(f.getAbsolutePath(), FileUtils.S_IRUSR | FileUtils.S_IWUSR | FileUtils.S_IRGRP | FileUtils.S_IROTH, -1, -1) ;

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.