[ Version ( since = "2.22" ) ]
public abstract FileIOStream open_readwrite (Cancellable? cancellable = null) throws Error

Opens an existing file for reading and writing.

The result is a FileIOStream that can be used to read and write the contents of the file.

If cancellable is not null, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error g_io_error_cancelled will be returned.

If the file does not exist, the g_io_error_not_found error will be returned. If the file is a directory, the g_io_error_is_directory error will be returned. Other errors are possible too, and depend on what kind of filesystem the file is on. 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: Open an existing file for 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.open_readwrite (); (0, SeekType.END);

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

return 0;

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



File to open


a Cancellable


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