Reading and writing text files

hi,
I haven’t been able to find any sample code, tutorial, or docs which explain how to use the FileSystem in Heaps.

The Resources page tells me that I need to init the local fs via “hxd.Res.initLocal();”, but inserting that code into my init() function generates a “no such file or directory” error (despite that I’m not yet asking to load any files!).

I can access sys.FileSystem but this doesn’t seem to implement FileSystem (ie dir() is missing). sys.FileSystem.readDirectory() is giving me errors no matter how I format the path.

Anyway, if anyone has any pointers or tips on how to read/write local text files, please let me know! I just want to get a list of file names, read them as bytes, and write new files as bytes. I know this shouldn’t be very complex, and I’ve done this before in several other languages without problem, but those languages all provided docs which had working sample code that demonstrated exactly how to correctly use the file system objects.

I’m really enjoying programming with Heaps but the lack of docs is definitely somewhat frustrating. :confused:

thanks,
Raigan

Hi!
I’m new to this, too. This is a tiny class for reading the directory:

import sys.FileSystem;

class ListFilesInDirectory {

	static public function main():Void {
		
		listallfiles();
	}

	static public function listallfiles():Void {

		trace("Check directory for files...");
		var allFilesArray = FileSystem.readDirectory("");

		// list all files in directory
		trace("These files have been found:");
		for (f in allFilesArray) {
			trace(" -> " + f.toString());
		}
		trace("All files have been listed.");
	}
}

So, actually you would fall back to the Haxe language itself; there is no “direct need” for Heaps.
Further resources:
https://api.haxe.org/sys/FileSystem.html

Here comes the same program implemented in Heaps:

import sys.FileSystem;
import hxd.res.Font;

class TestApp_Files extends hxd.App {

    override function init(){
        var myText = new h2d.Text( hxd.res.DefaultFont.get() , s2d );
        myText.textColor = 0x0000FF;
        myText.text = "";

        trace("Check directory for files...");
        var allFilesArray = FileSystem.readDirectory("");
        // list all files in directory
        trace("These files have been found:");
        for (f in allFilesArray) {
            myText.text += "\n -> " + f.toString();
        }
        trace("All files have been listed.");
    }

    static function main() {
        new TestApp_Files();
    }
}

This should read a file in the same directory called some_file_i_would_like_to_read.txt.
It is implemented in a Heaps app, too:

import sys.io.File;

class TestApp_ReadFile extends hxd.App {

    override function init(){
        var myText = new h2d.Text( hxd.res.DefaultFont.get() , s2d );
        myText.textColor = 0x0000FF;
        myText.text = File.getContent( "some_file_i_would_like_to_read.txt" );
    }

    static function main() {
        new TestApp_ReadFile();
    }

}

https://api.haxe.org/sys/io/File.html

Thank you so much! This really helps. :slight_smile:

1 Like