File Reading

117 views
Skip to first unread message

Artem Kolesnik

unread,
Jan 26, 2017, 4:09:44 PM1/26/17
to DroidScript
Another question: how can I read file line-by-line?
Message has been deleted

Symbroson Development

unread,
Jan 26, 2017, 4:14:11 PM1/26/17
to DroidScript
(Sry previous was wrong 😅)

Just use str = app.ReadFile("filepath");
You can get an array of the lines that way:
lines = str.split('\n');

Hope this helps

Steve Garman

unread,
Jan 26, 2017, 4:30:34 PM1/26/17
to DroidScript
I would normally use the method suggested above.

However if the file is very large or you have some other good reason to read it line by line, you can open the file for random access.

var fil = "/sdcard/sh_out.txt";
//Called when application is started.
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
//Create a text label and add it to layout.
txt = app.CreateText( "",0.9,0.5,"log" );
lay.AddChild( txt );

//Create a button
btn = app.CreateButton( "Read" );
btn.SetOnTouch( readLine );
lay.AddChild( btn );

//Add layout to app.
app.AddLayout( lay );

//Open file for reading
file1 = app.CreateFile( fil,"r" );
readLine();
}

function readLine()
{
var s = file1.ReadText("Line");
txt.Log( s );
}

Artem Kolesnik

unread,
Jan 27, 2017, 8:50:34 AM1/27/17
to DroidScript
Thanks Steve and Symbroson!
Reply all
Reply to author
Forward
0 new messages