Copy folders in Java and set file permissions in Android

Source: Internet
Author: User

Code for copying folders:

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 [1, 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;

}

 

There are two ways to set folder permissions on Android:

1:

Try {
String command = "chmod 644" + F. getabsolutepath ();
Runtime runtime = runtime. getruntime ();
Java. Lang. Process proc = runtime.exe C (command );
} Catch (ioexception e ){
E. printstacktrace ();
}

2:

When fileutils is used, this class is hidden by default. The official SDK does not contain this class. Therefore, to use this class for code, you need to compile the project in the android source code.
Fileutils. setpermissions (F. getabsolutepath (), fileutils. s_irusr | fileutils. s_iwusr | fileutils. s_irgrp | fileutils. s_iroth,-1,-1 );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.