Page 1 of 1

reading first lines of a TXT file

PostPosted: Tue May 30, 2006 11:28 am
by Harjo
Hi,

I want to read the first 3 or 4 lines of a 15MB TXT file.
Can I do this without loading the whole file into memory/variable?

PostPosted: Tue May 30, 2006 11:33 am
by Jan Blok
var fr = new Packages.java.io.FileReader('/tmp/xyz.log');
var br = new Packages.java.io.BufferedReader(fr);
global.my3lines += br.readLine();
global.my3lines += br.readLine();
global.my3lines += br.readLine();
br.close();//do NOT forget this close! to prevent mem leaks, even better to use try/finally here

In Java you can do anything, its only a bit more complicated :wink:

PostPosted: Tue May 30, 2006 11:36 am
by Harjo
very powerful stuff!

thanks! :D

Re: reading first lines of a TXT file

PostPosted: Thu Jan 05, 2012 3:49 pm
by Karel Broer
Great tip Jan! What is the syntax to loop through all lines of the file?

Re: reading first lines of a TXT file

PostPosted: Thu Jan 05, 2012 3:51 pm
by Jan Blok
...
var line = null;
while ((line = br.readLine()) != null)
{
globals.my3lines += line;
}
br.close();

Re: reading first lines of a TXT file

PostPosted: Thu Jan 05, 2012 4:06 pm
by ROCLASI
I recently found out that this doesn't read files in with the proper encoding (in my case UTF-8).
The following code does:

Code: Select all
function readFile(_oFile) {
    //
    // Use BufferedReader so we don't have to read the whole file into memory
    //
    var _oFR = new Packages.java.io.FileInputStream(_oFile),
        _oIR = new Packages.java.io.InputStreamReader(_oFR, "UTF8"),
        _oBR = new Packages.java.io.BufferedReader(_oIR),
        _sLine = "dummy",
        _nReadLine = 0;
   
    // using a database transaction (might/will) speed things up
    databaseManager.startTransaction();
   
    try {
        while (_sLine) {
            _sLine = _oBR.readLine();
            _nReadLine++;
   
            if (_sLine) {
   
                // Put your processing code here
            }
        }
        // Save any unsaved data
        databaseManager.saveData();
   
        //
        //do NOT forget this close! to prevent memory leaks
        //
        _oBR.close();
       
        // Close the database transaction
        databaseManager.commitTransaction();
   
    } catch (_oErr) {
        _oBR.close();
        application.output("ERROR: " + _oFile.getName() + " at row " + _nReadLine, LOGGINGLEVEL.ERROR);
        application.output("ERROR: " + _oErr, LOGGINGLEVEL.ERROR);
        databaseManager.rollbackTransaction();
        return; // stop process
    }
   
    //
    // garbage collection
    //
    _oFR = null;
    _oIR = null;
    _oBR = null;
}


Hope this helps.

Re: reading first lines of a TXT file

PostPosted: Thu Jan 05, 2012 6:13 pm
by Karel Broer
ROCLASI wrote:I recently found out that this doesn't read files in with the proper encoding (in my case UTF-8).
The following code does. Hope this helps.

It did help quite a bit! Thanks Robert!