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