reading first lines of a TXT file

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?

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:

very powerful stuff!

thanks! :D

Great tip Jan! What is the syntax to loop through all lines of the file?


var line = null;
while ((line = br.readLine()) != null)
{
globals.my3lines += line;
}
br.close();

I recently found out that this doesn’t read files in with the proper encoding (in my case UTF-8).
The following code does:

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.

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. Hope this helps.

It did help quite a bit! Thanks Robert!