reading first lines of a TXT file

Questions, tips and tricks and techniques for scripting in Servoy

reading first lines of a TXT file

Postby Harjo » Tue May 30, 2006 11:28 am

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?
Harjo Kompagnie
byKom B.V.
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Postby Jan Blok » Tue May 30, 2006 11:33 am

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:
Jan Blok
Servoy
Jan Blok
 
Posts: 2684
Joined: Mon Jun 23, 2003 11:15 am
Location: Amsterdam

Postby Harjo » Tue May 30, 2006 11:36 am

very powerful stuff!

thanks! :D
Harjo Kompagnie
byKom B.V.
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Re: reading first lines of a TXT file

Postby Karel Broer » Thu Jan 05, 2012 3:49 pm

Great tip Jan! What is the syntax to loop through all lines of the file?
Karel Broer
ServoyCamp - http://www.servoycamp.com
User avatar
Karel Broer
 
Posts: 779
Joined: Mon May 03, 2004 12:49 am
Location: Doetinchem

Re: reading first lines of a TXT file

Postby Jan Blok » Thu Jan 05, 2012 3:51 pm

...
var line = null;
while ((line = br.readLine()) != null)
{
globals.my3lines += line;
}
br.close();
Jan Blok
Servoy
Jan Blok
 
Posts: 2684
Joined: Mon Jun 23, 2003 11:15 am
Location: Amsterdam

Re: reading first lines of a TXT file

Postby ROCLASI » Thu Jan 05, 2012 4:06 pm

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.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: reading first lines of a TXT file

Postby Karel Broer » Thu Jan 05, 2012 6:13 pm

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!
Karel Broer
ServoyCamp - http://www.servoycamp.com
User avatar
Karel Broer
 
Posts: 779
Joined: Mon May 03, 2004 12:49 am
Location: Doetinchem


Return to Methods

Who is online

Users browsing this forum: No registered users and 2 guests

cron