User Tools

Site Tools


Sidebar

Privacy Policy

News

Version 2.50 is out since Jan 1st 2022


Frequently Asked Questions


Namespaces

Note for contributors

If you wish to create a new page in the DroidScript wiki, please click on the most appropriate namespace above and follow the notes for contributors there.

Because of spam, it has been necessary to add a CAPTCHA to the registration form and the save option for editing pages. You will not need to prove you are human if you are logged in, so please register.

Please feel free to improve any existing page, as well as adding new pages to increase the sum of public knowledge about DroidScript.

Formatting Syntax

sample_code:random_file_acces

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:

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

And here are some Java code snippets which will have to serve as documentation for now:

        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 ) 
 
	//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
	public String ReadText( String type ) 
 
	//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 ) 

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)