Android copy files in assets directory to the specified directory

Source: Internet
Author: User

1      PackageCom.android.demo;2 3     ImportJava.io.File;4     ImportJava.io.FileOutputStream;5     ImportJava.io.InputStream;6     ImportAndroid.content.Context;7      Public classCopyfilefromassets {8     /**9 *Ten *@paramMycontext One *@paramassets_name The file name to copy A *@paramSavepath the path to save - *@paramsavename The file name after the copy - * Testcopy (context context) is a test example.  the     */ -      Public Static voidcopy (Context mycontext, String assets_name, - string Savepath, String savename) { -String filename = savepath + "/" +Savename; +File dir =NewFile (savepath); -     //If the directory does not exist, create this directory +     if(!dir.exists ()) A Dir.mkdir (); at     Try { -     if(! (NewFile (filename)). Exists ()) { -InputStream is =mycontext.getresources (). Getassets () - . Open (assets_name); -FileOutputStream fos =Newfileoutputstream (filename); -     byte[] buffer =New byte[7168]; in     intCount = 0; -      while((count = is.read (buffer)) > 0) { toFos.write (buffer, 0, count); + } - fos.close (); the is.close (); * } $}Catch(Exception e) {Panax Notoginseng e.printstacktrace (); - } the } +      Public voidtestcopy (Context context) { AString path=Context.getfilesdir (). GetAbsolutePath (); theString name= "Test.txt"; + copyfilefromassets.copy (context, name, path, name); - } $}

(ext: http://bbs.9ria.com/thread-232474-1-1.html)

Android copy files in assets directory to the specified directory

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.