Append text on file

Hello to all

//My first try
var fileDir:String= "C:/superfile.txt";
File.write(fileDir,false);
var myList = new haxe.ds.List<String>();
myList.add("bla");
myList.add("bli");

File.append(fileDir,true); //not working

//What I found
      public function append(message:String, fileName:String) {
        var output:FileOutput = sys.io.File.append(fileName, false);
        output.writeString(message);
        output.close();
      }

Are we always obliged to do so complicate,
why this is not working:

File.append(fileDir,true);

I am obliged to interpret this… output open a door on the file, write on it, and close it after??

sys.io.File.append returns a FileOutput object which you can use to add content and close the file after:

// Create a handle to the file in text format (the false, true is binary)
var file = File.append("C:/superfile.txt", false);

// Add content to the file
file.writeString("bla\r\n");
file.writeString("bli\r\n");

// Close the file
file.close();

File.write is similar but replaces the file instead of appending it.

Reading and writing have their shortcut as File.getContent and File.saveContent, but not appending, so you’ll have to make your own utility function like the example you found.

1 Like

@ibilon A great thanks for your clear and instructive reply!

1 Like