File IO
This content is for Haxe 3.0 and is not currently available in this form, this page is work in progress and code is untested.
Introduction
This tutorial covers simple ascii and binary file input and output using the Haxe with 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; #if neko import neko.Lib; #else if php import php.Lib; #else if cpp import cpp.Lib; #end 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); neko.Lib.println("file content:\n" + fileContent); // open and read file line by line var fin = File.read(fname, false); try { Lib.println("file content:"); var lineNum = 0; while( true ) { var str = fin.readLine(); Lib.println("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; #if neko import neko.Lib; #else if php import php.Lib; #else if cpp import cpp.Lib; #end import haxe.Int32; class WriteReadBinary { static function main() { var fname = "binary_file.dat"; // open file for writing var fout = 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 = 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 Lib.println("read int: " + intVal); Lib.println("read int32: " + haxe.Int32.toInt(int32Val)); Lib.println("read double: " + doubleVal); 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. - need to check how this tutorial should change for cross platform.
To compile and run for the neko target 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.