Reading a file line by line?

326 views
Skip to first unread message

samkm

unread,
Aug 17, 2018, 8:37:48 AM8/17/18
to DroidScript
Hey peeps,
Sorry if what I'm asking here is really obvious, I'm pretty new to Droidscript and Javascript! Basically, I have a huge file (50mb) I need to parse data from - trying to load it all at once with app.ReadFile just freezes the program! Is there any way to get data from the file line by line? Shell commands maybe? I was reading something about streams and the FS library in javascript, is there any way to use these in Droidscript?
Thanks for any help ^_^ x
Message has been deleted

Steve Garman

unread,
Aug 17, 2018, 9:35:14 AM8/17/18
to DroidScript
You can open your file for random access and use ReadText() to read it line by line http://wiki.droidscript.me.uk/doku.php?id=sample_code:random_file_acces

It is not as user-friendly as most DroidScript code but it will seem really obvious to some people and really difficult to others.

I recommend you try it and if it doesn't make sense to you, come back and ask here with the code that you are having troubles with.

CHD

unread,
Aug 17, 2018, 10:04:40 AM8/17/18
to DroidScript
Well,
I suggest creating a plugin for this...
Plugin will read file and return a string which the DS will read instantly...

Steve suggested a better idea doing this from DS. This process works well to some extent. It reads the file line by line so it will again take very long time to completely read the file specially when it is 50mb or above...

Besides,
The android may alert 'app not responding dialog'...

You need to add progress to prevent this...
Good luck!

Steve Garman

unread,
Aug 17, 2018, 10:21:29 AM8/17/18
to DroidScript
If you really need to read and parse it line by line, you can use something like this
var file,len

function OnStart()
{
 
var res
 console
.log(app.FileExists("/sdcard/Download/Untitled7.txt"))

 file
= app.CreateFile( "/sdcard/Download/Untitled 7.txt", "r" );
 
 len
= file.GetLength();
 console
.log( "file len: " + len );
  file
.Seek( 0 );
 
 
while(true)
 
{
     res
= readNext();
     
if(typeof res == "undefined")
     
{
         alert
( "Finished" );
         
break;
     
}
     alert
( res )
 
}
 
}
 
function readNext()
{
   
var lin;
   
if(file.GetPointer() < len)
   
{
        lin
= file.ReadText( "Line" );
   
}
   
else
   
{
        file
.Close();
        lin
= undefined;
   
}
   
return( lin )
}

However CHD is right that it would be very slow to do a file that size in any way that requires a call over the JavaScript<==>Java bridge for every line.

On the other hand if you can do all the parsing in pure JavaScript, you may be able to get a reasonable speed by reading the file in much larger chunks.
Message has been deleted

Jared

unread,
Aug 17, 2018, 11:27:37 AM8/17/18
to DroidScript
Read the file in and save to a variable through the app.ReadFile() function. Then take that text and split it using the following regex:
var text = app.ReadFile(fileName).split(/\r\n|\r|\n/gm)


then to access a line, you just set a variable to the index (which SHOULD be the line number + 1) if I'm not mistaken. for example :

//get 10th line
//(using text var from above example)
var line = 10
app.ShowPopup(text[line + 1])

This is based off an extremely elaborate keyboard shortcut method I have been utilizing in an app I'm working on, and so as I cut out a tiny part of it to try and apply to this question, that above code may or may not work for you in the context of your situation.

Jared

unread,
Aug 17, 2018, 11:32:49 AM8/17/18
to DroidScript
So I tested it...

it works, but change the following :

text[line+1]

should actually be :

text[(line+1)]

alex.symbroson

unread,
Aug 17, 2018, 12:42:31 PM8/17/18
to DroidScript
no, it should be text[line-1] if line is an index counter starting from 1

Jared

unread,
Aug 17, 2018, 1:30:06 PM8/17/18
to DroidScript
I blame an entire semester of python... Lol

samkm

unread,
Aug 17, 2018, 5:59:21 PM8/17/18
to DroidScript
That looks perfect! Thank you so much, I'll have a play with it ^_^
Just so I know for sure, by the javascript-java bridge, are you saying it's slow to use the file read/write commands? Does that go for most/all of the built in methods?
This is a bit off topic but I'm just curious - can Droidscript load and use javascript modules (like FS), or is that something that'd need to be added through a plugin? Again, really sorry if I'm asking stupid questions!
Thank you all so much for the help!
Reply all
Reply to author
Forward
0 new messages