Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

FileAccess

Inherits: RefCounted < Object

Provides methods for file reading and writing operations.

Description

This class can be used to permanently store data in the user device's file system and to read from it. This is useful for store game save data or player configuration files.

Here's a sample on how to write and read from a file:

func save(content):
    var file = FileAccess.open("user://save_game.dat", FileAccess.WRITE)
    file.store_string(content)

func load():
    var file = FileAccess.open("user://save_game.dat", FileAccess.READ)
    var content = file.get_as_text()
    return content

In the example above, the file will be saved in the user data folder as specified in the Data paths documentation.

FileAccess will close when it's freed, which happens when it goes out of scope or when it gets assigned with null. close can be used to close it before then explicitly. In C# the reference must be disposed manually, which can be done with the using statement or by calling the Dispose method directly.

Note: To access project resources once exported, it is recommended to use ResourceLoader instead of FileAccess, as some files are converted to engine-specific formats and their original source files might not be present in the exported PCK package.

Note: Files are automatically closed only if the process exits "normally" (such as by clicking the window manager's close button or pressing Alt + F4). If you stop the project execution by pressing F8 while the project is running, the file won't be closed as the game process will be killed. You can work around this by calling flush at regular intervals.

Tutorials

Properties

bool

big_endian

Methods

void

close ( )

bool

eof_reached ( ) const

bool

file_exists ( String path ) static

void

flush ( )

int

get_8 ( ) const

int

get_16 ( ) const

int

get_32 ( ) const

int

get_64 ( ) const

String

get_as_text ( bool skip_cr=false ) const

PackedByteArray

get_buffer ( int length ) const

PackedStringArray

get_csv_line ( String delim="," ) const

float

get_double ( ) const

Error

get_error ( ) const

PackedByteArray

get_file_as_bytes ( String path ) static

String

get_file_as_string ( String path ) static

float

get_float ( ) const

bool

get_hidden_attribute ( String file ) static

int

get_length ( ) const

String

get_line ( ) const

String

get_md5 ( String path ) static

int

get_modified_time ( String file ) static

Error

get_open_error ( ) static

String

get_pascal_string ( )

String

get_path ( ) const

String

get_path_absolute ( ) const

int

get_position ( ) const

bool

get_read_only_attribute ( String file ) static

float

get_real ( ) const

String

get_sha256 ( String path ) static

BitField<UnixPermissionFlags>

get_unix_permissions ( String file ) static

Variant

get_var ( bool allow_objects=false ) const

bool

is_open ( ) const

FileAccess

open ( String path, ModeFlags flags ) static

FileAccess

open_compressed ( String path, ModeFlags mode_flags, CompressionMode compression_mode=0 ) static

FileAccess

open_encrypted ( String path, ModeFlags mode_flags, PackedByteArray key ) static

FileAccess

open_encrypted_with_pass ( String path, ModeFlags mode_flags, String pass ) static

void

seek ( int position )

void

seek_end ( int position=0 )

Error

set_hidden_attribute ( String file, bool hidden ) static

Error

set_read_only_attribute ( String file, bool ro ) static

Error

set_unix_permissions ( String file, BitField<UnixPermissionFlags> permissions ) static

void

store_8 ( int value )

void

store_16 ( int value )

void

store_32 ( int value )

void

store_64 ( int value )

void

store_buffer ( PackedByteArray buffer )

void

store_csv_line ( PackedStringArray values, String delim="," )

void

store_double ( float value )

void

store_float ( float value )

void

store_line ( String line )

void

store_pascal_string ( String string )

void

store_real ( float value )

void

store_string ( String string )

void

store_var ( Variant value, bool full_objects=false )


Enumerations

enum ModeFlags:

ModeFlags READ =