[ CCode ( vfunc_name = "read_fn" ) ]
public abstract FileInputStream read (Cancellable? cancellable = null) throws Error
Opens a file for reading.
The result is a FileInputStream that can be used to read 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.
Example: Read, sync:
public static int main (string[] args) {
File file = File.new_for_path ("my-test.txt");
try {
FileInputStream @is = file.read ();
DataInputStream dis = new DataInputStream (@is);
string line;
while ((line = dis.read_line ()) != null) {
print ("%s\n", line);
}
} catch (Error e) {
print ("Error: %s\n", e.message);
}
return 0;
}
valac --pkg gio-2.0 GLib.File.read.vala
this |
File to read |
cancellable |
FileInputStream or null on error. Free the returned object with unref. |