Android reads files under asserts and raw folders
Often need to read "/res/raw" and "/asserts" folders under the file, simply written as a tool to facilitate later use.
One, the raw folder under the file Operation Tool class:
1 /**2 * File Processing tool class under the Raw folder3 * 4 * */5 Public classRawfileutils {6 Privaterawfileutils () {7 8 }9 Ten /** One * Read files under the raw folder A * @paramresourceId the file resource ID under the raw folder - * @returnFile Contents - * the * */ - Public StaticString Readfilefromraw (Context context,intresourceId) { - if(NULL= = Context | | ResourceId < 0 ){ - return NULL; + } - +String result =NULL; A Try { atInputStream InputStream =context.getresources (). Openrawresource (resourceId); - //Gets the number of bytes in the file - intLength =inputstream.available (); - //Create a byte array - byte[] buffer =New byte[length]; - //read the data in a file into a byte array in inputstream.read (buffer); -result = encodingutils.getstring (buffer, "Utf-8"); to}Catch(Exception e) { + e.printstacktrace (); - } the * returnresult; $ }Panax Notoginseng}
Second, theasserts folder under the file Operation Tool class:
1 /**2 * asserts file processing3 * 4 * */5 Public classAssertsfileutils {6 Privateassertsfileutils () {7 8 }9 Ten /** One * Read the files in the asserts directory A * @paramfileName eg: "Updatelog.txt" - * @returnthe contents of the corresponding file - * the * */ - Public StaticString Readfilefromassets (context context, string fileName)throwsIOException, illegalargumentexception { - if(NULL= = Context | |Textutils.isempty (FileName)) { - Throw NewIllegalArgumentException ("Bad arguments!" ); + } - +Assetmanager Assetmanager =context.getassets (); AInputStream input =Assetmanager.open (fileName); atBytearrayoutputstream output =NewBytearrayoutputstream (); - byte[] buffer =New byte[1024]; - intLength = 0; - while(length = input.read (buffer))! =-1) { -Output.write (buffer, 0, length); - } in output.close (); - input.close (); to + returnoutput.tostring (); - } the * /** $ * List all files under the asserts folderPanax Notoginseng * @returnList of file names under the asserts directory - * the * */ + Public StaticList<string> Getassertsfiles (Context context)throwsillegalargumentexception{ A if(NULL==context) { the Throw NewIllegalArgumentException ("Bad arguments!" ); + } - $Assetmanager Assetmanager =context.getassets (); $string[] Files =NULL; - Try { -Files = Assetmanager.list (""); the}Catch(IOException e) { - e.printstacktrace ();Wuyi } the - return(NULL= = files)?NULL: arrays.aslist (files); Wu } -}
Third, examples :
1 Public classMyActivityextendsactivity{2 3 Public Static FinalString ENCODING = "UTF-8";4 TextView TV1;5 TextView TV2;6 7 @Override8 protected voidonCreate (Bundle savedinstancestate) {9 Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.main); OneTV1 =(TextView) Findviewbyid (R.ID.TV1); A Tv1.settextcolor (color.red); -Tv1.settextsize (15.0f); -TV2 =(TextView) Findviewbyid (R.ID.TV2); the Tv2.settextcolor (color.red); -Tv2.settextsize (15.0f); - Tv1.settext (Getfromraw ()); -Tv2.settext (Getfromassets ("Test2.txt")); + } - + //get files and read data from the raw folder in resources A PublicString Getfromraw () { atString result = ""; - Try { -InputStream in =getresources (). Openrawresource (r.raw.test1); - //Gets the number of bytes in the file - intLenght =in.available (); - //Create a byte array in byte[] buffer =New byte[lenght]; - //read the data in a file into a byte array to in.read (buffer); +result =encodingutils.getstring (buffer, ENCODING); -}Catch(Exception e) { the e.printstacktrace (); * } $ returnresult;Panax Notoginseng } - the //get the file from the Assets folder and read the data + Publicstring Getfromassets (String fileName) { AString result = ""; the Try { +InputStream in =getresources (). Getassets (). open (fileName); - //Gets the number of bytes in the file $ intLenght =in.available (); $ //Create a byte array - byte[] buffer =New byte[lenght]; - //read the data in a file into a byte array the in.read (buffer); -result =encodingutils.getstring (buffer, ENCODING);Wuyi}Catch(Exception e) { the e.printstacktrace (); - } Wu returnresult; - } About}
Reference: http://blog.csdn.net/ekeuy/article/details/39479201
Android reads files under asserts and raw folders