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 ();}