File IO

This content is for Haxe 3.0 and has been tested on the Java target but should work the same on other targets with file access accept possible nodejs.

Introduction


This tutorial covers simple ascii and binary file input and output using the Haxe with java/c#/neko/php/cpp libraries. The sys.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 sys.io.File;
import sys.io.FileInput;
import sys.io.FileOutput;

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

    // open file for writing
    var fout = 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 = File.getContent( fname );
    trace( "file content:\n" + fileContent );

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

We are using sys.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 sys.io.FileOutput and sys.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.

To compile and run for the neko target 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 sys.io.File;
import sys.io.FileInput;
import sys.io.FileOutput;
class WriteReadBinary
{
  static function main()
  {
    var fname = "binary_file.dat";

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

    // write something
    fout.writeByte( 202 );
    fout.writeDouble( 10.01 );
    fout.writeString( "kayak" );
    fout.close();

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

    // output values read from file 
    trace("read int32: " + int32Val );
    trace("read double: " + doubleVal);
    trace("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.

To compile and run for the neko target with:

haxe -neko writereadbinary.n -main WriteReadBinary
neko writereadbinary

please see the hello world examples for each target to see how to setup the hxml file for the specific target.

The output should be:

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 #19640, modified 2013-08-12 07:55:22 by JLM