This page is an archive of my old blog. Please visit for my current blog.
This site is no longer being maintained and commenting is disabled.

AIR Tip 13 – Number of Lines in a Text File

I had a situation today where another developer needed to know how to determine how many lines were in a text file with AIR. Especially for developers that are not used to working with binary data - this could prove difficult. Making a solution that worked consistently across multiple platforms would be even more challenging as some OS's use different 'end line' characters. However, this is another area where AIR helps you out.

The File Class's Static Properties

If you want to work with the user's local filesystem at all, you will be using instances of the File class. This class also provides static properties that help you create consistent experiences across multiple operating systems. For example, there are several properties that map to specific directories on a user's computer (documentsDirectory will map correctly whether you are on Mac or PC). In addition, there is a property File.separator which displays the current path separator (so you don't have to worry if it is '/' or '\'). For our example today, we will be using File.lineEnding which will correspond to the correct line ending characters for your platform.

Once we have this is place, we can easily determine the number of lines in a text file by following these steps:

  1. Allow the user to select a file on their hard drive.
  2. Read the UTFBytes into a string.
  3. Split that string by the File.lineEnding property into an Array.
  4. Use the length of the Array to determine the number of lines.

In the following example, those items are accomplished. This is a Flex-based AIR application, but it could easily be modified to work with Flash.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:WindowedApplication xmlns:mx=""
  3.     layout="vertical"
  4.     creationComplete="handleCreationComplete()">
  6. <mx:Script>
  7.     <![CDATA[
  9.         import flash.filesystem.File;
  10.         import flash.filesystem.FileMode;
  11.         import flash.filesystem.FileStream;
  13.         private var file:File;
  14.         [Bindable]
  15.         private var lines:uint = 0;
  17.         protected function handleCreationComplete():void
  18.         {
  19.             file = new File();
  20.             file.addEventListener( Event.SELECT, handleFileSelect );
  21.         }
  23.         protected function handleFileSelect( event:Event ):void
  24.         {
  25.             lines = 0;
  26.             file = as File;
  27.             var stream:FileStream = new FileStream();
  28.    file, FileMode.READ );
  29.             var str:String = stream.readUTFBytes(stream.bytesAvailable);
  30.             stream.close();
  31.             var oa:Array = str.split( File.lineEnding );
  32.             lines = oa.length;
  33.             filePath.text = file.nativePath;
  34.         }
  36.     ]]>
  37. </mx:Script>
  39. <mx:HBox width="100%" horizontalAlign="center">
  40.     <mx:Button label="Browse" click="file.browse()" />
  41.     <mx:TextInput id="filePath" width="100%" />
  42. </mx:HBox>
  44. <mx:Label text="Number of Lines: {lines}" />
  46. </mx:WindowedApplication>

One Response to “AIR Tip 13 – Number of Lines in a Text File”