[ Version ( since = "2.22" ) ]
public abstract FileIOStream replace_readwrite (string? etag, bool make_backup, FileCreateFlags flags, Cancellable? cancellable = null) throws Error

Returns an output stream for overwriting the file in readwrite mode, possibly creating a backup copy of the file first.

If the file doesn't exist, it will be created.

For details about the behaviour, see replace which does the same thing but returns an output stream only.

Note that in many non-local file cases read and write streams are not supported, so make sure you really need to do read and write streaming, rather than just opening for reading or writing.

Example: Get a stream to override a file in r/w, sync:

public static int main (string[] args) {
if (args.length != 2) {
print ("%s FILE\n", args[0]);
return 0;

try {
File file = File.new_for_commandline_arg (args[1]);
FileIOStream iostream = file.replace_readwrite (null, false, FileCreateFlags.NONE);

OutputStream ostream = iostream.output_stream;
DataOutputStream dostream = new DataOutputStream (ostream);
dostream.put_string ("my-line\n");
} catch (Error e) {
print ("Error: %s\n", e.message);

return 0;

valac --pkg gio-2.0 GLib.File.replace_readwrite.vala



a File


an optional [entity tag](entity-tags) for the current File, or NULL to ignore


true if a backup should be created


a set of FileCreateFlags


optional Cancellable object, null to ignore


a FileIOStream or null on error. Free the returned object with unref.