# reading first lines of a TXT file

**URL:** <https://forum.servoy.com/t/reading-first-lines-of-a-txt-file/6006>\
**Category:** Classic Servoy\
**Created:** [May 30, 2006, 9:28am UTC](https://forum.servoy.com/t/reading-first-lines-of-a-txt-file/6006 "2006-05-30T09:28:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harjo](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/harjo/32/4873_2.png) [@Harjo](https://forum.servoy.com/u/Harjo)\
**Post date:** [May 30, 2006, 9:28am UTC](https://forum.servoy.com/t/reading-first-lines-of-a-txt-file/6006/1 "2006-05-30T09:28:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Jan\_Blok](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@Jan\_Blok](https://forum.servoy.com/u/Jan_Blok)\
**Post date:** [May 30, 2006, 9:33am UTC](https://forum.servoy.com/t/reading-first-lines-of-a-txt-file/6006/2 "2006-05-30T09:33:04Z")

</div>

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:]( "Wink")

---

<div class="post-metadata">

**Author:** ![Harjo](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/harjo/32/4873_2.png) [@Harjo](https://forum.servoy.com/u/Harjo)\
**Post date:** [May 30, 2006, 9:36am UTC](https://forum.servoy.com/t/reading-first-lines-of-a-txt-file/6006/3 "2006-05-30T09:36:48Z")

</div>

very powerful stuff!

thanks! ![:D]( "Very Happy")

---

<div class="post-metadata">

**Author:** ![Karel\_Broer](https://avatars.discourse-cdn.com/v4/letter/k/bcef8e/32.png) [@Karel\_Broer](https://forum.servoy.com/u/Karel_Broer)\
**Post date:** [January 5, 2012, 1:49pm UTC](https://forum.servoy.com/t/reading-first-lines-of-a-txt-file/6006/4 "2012-01-05T13:49:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jan\_Blok](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@Jan\_Blok](https://forum.servoy.com/u/Jan_Blok)\
**Post date:** [January 5, 2012, 1:51pm UTC](https://forum.servoy.com/t/reading-first-lines-of-a-txt-file/6006/5 "2012-01-05T13:51:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ROCLASI](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/roclasi/32/4371_2.png) [@ROCLASI](https://forum.servoy.com/u/ROCLASI)\
**Post date:** [January 5, 2012, 2:06pm UTC](https://forum.servoy.com/t/reading-first-lines-of-a-txt-file/6006/6 "2012-01-05T14:06:56Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Karel\_Broer](https://avatars.discourse-cdn.com/v4/letter/k/bcef8e/32.png) [@Karel\_Broer](https://forum.servoy.com/u/Karel_Broer)\
**Post date:** [January 5, 2012, 4:13pm UTC](https://forum.servoy.com/t/reading-first-lines-of-a-txt-file/6006/7 "2012-01-05T16:13:03Z")

</div>

> 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!
