Skip to content

Instantly share code, notes, and snippets.

@MatthijsKamstra
Last active October 30, 2023 18:45
Show Gist options
  • Save MatthijsKamstra/15efab21735b970c93e251532596f1d6 to your computer and use it in GitHub Desktop.
Save MatthijsKamstra/15efab21735b970c93e251532596f1d6 to your computer and use it in GitHub Desktop.
Reading and writing to a file with Haxe
package;
/**
* Main class for reading and writing a simple text file.
* Author: Matthijs Kamstra aka [mck]
*/
class Main {
/**
* Constructor for the Main class.
* It demonstrates reading and writing operations on a text file.
*/
function new() {
// Print a message to the console
trace("Reading and writing example ๐Ÿค“ ๐Ÿ–Š๏ธ");
// Define a string that we want to write to the disk, including the current date
var str:String = 'Writing and reading a simple text file!\nWritten on: ' + Date.now().toString();
// Write the string to a file named 'hello.txt'
sys.io.File.saveContent('hello.txt', str);
// Now, let's read ๐Ÿ‘“ the file
var content = sys.io.File.getContent('hello.txt');
// Print the content of the file to the console
trace(content);
}
/**
* The main entry point for the application.
* If you want certain code to run automatically, you need to put it in
* a static main function and specify the class in the compiler arguments.
*/
static public function main() {
// Create an instance of the Main class
var main = new Main();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment