Read raw file content

Source: Internet
Author: User

Related links:

Android reads files
Http://www.eoeandroid.com/thread-97495-1-1.html

Writing and reading Android private folder files
Http://www.eoeandroid.com/thread-69378-1-1.html

Android reads File Content
Http://www.eoeandroid.com/thread-82295-1-1.html

---------------------- Post body -----------------------------

Asynctask, asynchronous processing, is mainly used to separate time-consuming operations from the main thread (ui thread) for processing, improving the Running Speed (smoothness ). I was going to learn about asynctask. When I saw the time-consuming operation, I considered reading large files and tried to read the files in the raw folder.
There are two ways to read, one is reading by row, and the other is reading by size;
MainCodeAs follows:
1. Read data by row

 Class Filereadtask Extends Asynctask <string, String, string>{String line_str; string result; Context context; filereadtask (context ){  This . Context = Context ;}  Int I = 0 ; @ Override  Protected  String doinbackground (string... Params) {bufferedreader Reader = New Bufferedreader ( New Inputstreamreader (context. getresources (). openrawresource (R. Raw. Test2 )));  Try  {  While (Line_str = reader. Readline ())! = Null &&! (Line_str = reader. Readline (). Equals ("" ) {Result + = Line_str; system. Out. println ( "Line_str:" +Line_str); publishprogress (line_str);} system. Out. println ( "Result:" + Result );}  Catch  (Ioexception e) {e. printstacktrace ();}  Return  Line_str ;}@ override  Protected   Void  Onpostexecute (string result ){ Super  . Onpostexecute (result) ;}@ override  Protected   Void  Onpreexecute (){  Super  . Onpreexecute () ;}@ override  Protected   Void  Onprogressupdate (string... values ){  Super . Onprogressupdate (Values [0]); System. Out. println ( "Values:" + values [0 ]); Array. Add (Values [ 0 ]); Adapter. notifydatasetchanged ();}} 

Ii. Read by size

Inputstream input = Context. getresources (). openrawresource (R. Raw. Test2); string result = Null  ;  Int  I;  Byte [] By = New  Byte [128]; //  Size of each read                   Try  {  While (I = input. Read (by)> 0 ){  Byte [] Bys = New   Byte  [I]; input. Read (bys, 0 , I); string s = New String (bys); Result + = S; system. Out. println (s );}}  Catch  (Ioexception e) {e. printstacktrace ();} 

 

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.