File IO

Introduction


This tutorial covers simple ascii and binary file input and output using the Haxe/neko libraries. The neko.io.File class provides a very simple yet powerful interface for working with files.

Ascii Files


In this example, we will create and write a few lines to an ascii file, then we will read what was written. HaXe provides two ways of reading ascii files. They can be opened and read a little at a time, or they can be read all at once. This example demonstrates both methods.

import haxe.io.Eof;
import neko.io.File;
import neko.Lib;

class WriteReadAscii
{
  static function main()
  {
    var fname = "ascii_file.txt";

    // open file for writing
    var fout = neko.io.File.write(fname, false);

    // write something
    fout.writeString("this is being written into the file\n");
    fout.writeString("this line includes a number: " + 123.312 + "\n");
    fout.close();

    // open and read file at once
    var fileContent = neko.io.File.getContent(fname);
    neko.Lib.println("file content:\n" + fileContent);

    // open and read file line by line
    var fin = neko.io.File.read(fname, false);
    try
    {
      neko.Lib.println("file content:");
      var lineNum = 0;
      while( true )
      {
    var str = fin.readLine();
    neko.Lib.println("line " + (++lineNum) + ": " + str);
      }
    }
    catch( ex:haxe.io.Eof ) 
    {}
    fin.close();
  }
}

We are using neko.io.File, haxe.io.Output, and haxe.io.Input from the standard library. haxe.io.Output and haxe.io.Input are the parent classes of neko.io.FileOutput and neko.io.FileInput, respectively.

Note that both the File.read and File.write methods take two parameters: the filename and a boolean flag where true indicates that the file is a binary file.

While we read the file line by line, the Input and Output classes also allow the file to be written or read by the character, byte or value for any basic type. When reading a file using the FileInput class, you must surround the read statement in a try catch block as was done in the example since there is no way of detecting an EOF. The FileInput.eof method seems like it might do this, but it doesn't.

Compile and run with:

haxe -neko writereadascii.n -main WriteReadAscii
neko writereadascii

The output should be:

file content:
this is being written into the file
this line includes a number: 123.312

file content:
line 1: this is being written into the file
line 2: this line includes a number: 123.312

The file "ascii_file.txt" should now exist in the current directory. It contains the same two lines.

Binary Files


In this example, we will create and write some data to a binary file, then we will read what was written. We are writing four values of different types: an int, a 32 bit int, a double, and a string.
import neko.io.File;
import neko.Lib;
import haxe.Int32;

class WriteReadBinary
{
  static function main()
  {
    var fname = "binary_file.dat";

    // open file for writing
    var fout = neko.io.File.write(fname, true);

    // write something
    fout.writeInt31(101);
    fout.writeInt32(haxe.Int32.ofInt(202));
    fout.writeDouble(10.01);
    fout.writeString("kayak");
    fout.close();

    // open and read each value
    var fin = neko.io.File.read(fname, true);
    var intVal = fin.readInt31();
    var int32Val = fin.readInt32();
    var doubleVal = fin.readDouble();
    var strVal = fin.readString(5);
    fin.close();

    // output values read from file
    neko.Lib.println("read int: " + intVal);
    neko.Lib.println("read int32: " + haxe.Int32.toInt(int32Val));
    neko.Lib.println("read double: " + doubleVal);
    neko.Lib.println("read string: " + strVal);
  }
}

Note that the length of the string must be known in order to read it. If it is not a fixed size string, it would be a good idea to store the string length in the file before the string.

Also note that there are no writeInt or readInt methods. We have to use the methods that specify the width of the int. Regular int's in neko are 31 bits, so the example has to convert neko int's for use with the writeInt32 and readInt32 methods.

Compile and run with:

haxe -neko writereadbinary.n -main WriteReadBinary
neko writereadbinary

The output should be:

read int: 101
read int32: 202
read double: 10.01
read string: kayak

The file "binary_file.dat" should now exist in the current directory. It contains the data in binary little endian format.

version #10245, modified 2011-03-04 14:37:46 by adnez