User Tools

Site Tools


sample_code:random_file_acces

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sample_code:random_file_acces [2015/07/08 09:18]
octazid created
sample_code:random_file_acces [2018/02/22 19:58]
Line 1: Line 1:
-====== Low Level Random File Access ====== 
  
-//(Posted by Dave Smart in the DroidScript-Betatesters google group)// 
- 
-Hi Guys, 
- 
-We were asked to include low level random file access by Henrik and decided to jam it in very quickly since others might also find it useful.  I'm not sure if we will publish docs on this since it is a bit hard core for most of our users, but I don't see any reason why it should not be documented in the Wiki ;) 
- 
-Here is some info about it: 
- 
-<code javascript> 
-function btn_OnTouch() 
-{ 
- var file = app.CreateFile( "/sdcard/test.txt", "rw" ); 
-  
- file.WriteText( "Hello", "Chars" ); 
- var len = file.GetLength(); 
- console.log( "file len: " + len ); 
-  
- file.Seek( len ); 
- file.WriteNumber( 77, "Byte" ); 
- file.WriteData( "66,121,101", "Int" ); 
-  
- file.Seek( 0 ); 
- var data = file.ReadData( 2 ); 
- console.log( "first two bytes:" + data[0] + "," + data[1] ); 
-  
- file.Seek( 0 ); 
- alert( file.ReadText( "Line" ) ); 
-  
- file.Close(); 
-} 
-</code> 
- 
-And here are some Java code snippets which will have to serve as documentation for now: 
- 
-<code java> 
- 
-        public void Close() 
- 
- public void Seek( long offset ) 
- 
- public long GetLength()  
- 
- public void SetLength( long len ) 
- 
- //Write text to file. 
- //types: "Bytes" => Writes the low order 8-bit bytes. 
- // "Chars" => Writes big-endian 16-bit characters. 
- // "UTF" => Writes text encoded with UTF-8. 
- public void WriteText( String data, String type ) throws Exception 
-  
- //Read text from file. 
- //types: "UTF" => reads a UTF-8 string with 2 byte header. 
- // "Line" => reads a whole text line terminated by '\n', '\r', "\r\n" 
- // "Char" => reads a big-endian 16-bit character 
- 
- //Write binary data to file. 
- //eg. "Hex":  "FA,FB,FC" 
- // "Int":  "250,251,252" 
- // "Text":  "abc46" => Writes the low order 8-bit bytes. 
- public void WriteData( String data, String mode ) throws Exception 
-  
- //Read binary data from file. 
- //Modes: "Hex", "Int", "Text" 
- public String ReadData( int len, String mode ) 
-  
- //Write numeric data to file (LE means little endian) 
- //types:  "Byte", "UByte", "Bool", "Float", "FloatLE", "Long", "LongLE",  
- //        "Short", "UShort", "ShortLE", "UShortLE", "Int", "UInt", "IntLE", "UIntLE" 
- public void WriteNumber( double data, String type ) 
-  
- 
- //Read numeric data from file  
- //types:  "Byte", "UByte", "Bool", "Float", "FloatLE", "Long", "LongLE",  
- //        "Short", "UShort", "ShortLE", "UShortLE", "Int", "UInt", "IntLE", "UIntLE" 
- public double ReadNumber( String type )  
-</code> 
- 
-I've just added file.GetPointer() and file.Skip()   
- 
-More infos: [[http://developer.android.com/reference/java/io/RandomAccessFile.html]] 
sample_code/random_file_acces.txt ยท Last modified: 2018/02/22 19:58 (external edit)