Snippet. Vala. Writing Text to File

This snippet shows how to write text to a file in Vala.

File file = File.new_for_path("Hello_World.txt");
try {
    if(file.query_exists() == true){
        file.delete(null);
        FileOutputStream fos = file.create (FileCreateFlags.REPLACE_DESTINATION, null);
        DataOutputStream dos = new DataOutputStream (fos);
        dos.put_string ("HELLO WORLD", null);
    }
} catch (Error e) {
    stderr.printf ("Error: %s\n", e.message);
}

Updated on: 03 Dec 2024