.. Generated automatically by doc/tools/makerst.py in Godot's source tree. .. DO NOT EDIT THIS FILE, but the doc/base/classes.xml source instead. .. _class_File: File ==== **Inherits:** :ref:`Reference` **<** :ref:`Object` **Category:** Core Brief Description ----------------- Type to handle file reading and writing operations. Member Functions ---------------- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`close` **(** **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`eof_reached` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`file_exists` **(** :ref:`String` path **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_16` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_32` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_64` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_8` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_as_text` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`RawArray` | :ref:`get_buffer` **(** :ref:`int` len **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`StringArray` | :ref:`get_csv_line` **(** :ref:`String` delim="," **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`float` | :ref:`get_double` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`get_endian_swap` **(** **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Error | :ref:`get_error` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`float` | :ref:`get_float` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_len` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_line` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_md5` **(** :ref:`String` path **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_modified_time` **(** :ref:`String` file **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_pascal_string` **(** **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_pos` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`float` | :ref:`get_real` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_sha256` **(** :ref:`String` path **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`get_var` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`is_open` **(** **)** const | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`open` **(** :ref:`String` path, :ref:`int` flags **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`open_encrypted` **(** :ref:`String` path, :ref:`int` mode_flags, :ref:`RawArray` key **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`open_encrypted_with_pass` **(** :ref:`String` path, :ref:`int` mode_flags, :ref:`String` pass **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`seek` **(** :ref:`int` pos **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`seek_end` **(** :ref:`int` pos=0 **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`set_endian_swap` **(** :ref:`bool` enable **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`store_16` **(** :ref:`int` value **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`store_32` **(** :ref:`int` value **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`store_64` **(** :ref:`int` value **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`store_8` **(** :ref:`int` value **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`store_buffer` **(** :ref:`RawArray` buffer **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`store_double` **(** :ref:`float` value **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`store_float` **(** :ref:`float` value **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`store_line` **(** :ref:`String` line **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`store_pascal_string` **(** :ref:`String` string **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`store_real` **(** :ref:`float` value **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`store_string` **(** :ref:`String` string **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`store_var` **(** :ref:`Variant` value **)** | +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ Numeric Constants ----------------- - **READ** = **1** --- Open the file for reading. - **WRITE** = **2** --- Open the file for writing. Create it if the file not exists and truncate if it exists. - **READ_WRITE** = **3** --- Open the file for reading and writing, without truncating the file. - **WRITE_READ** = **7** --- Open the file for reading and writing. Create it if the file not exists and truncate if it exists. - **COMPRESSION_FASTLZ** = **0** --- Use the FastLZ compression method. - **COMPRESSION_DEFLATE** = **1** --- Use the Deflate compression method. - **COMPRESSION_ZSTD** = **2** --- Use the Zstd compression method. - **COMPRESSION_GZIP** = **3** Description ----------- File type. This is used to permanently store data into the user device's file system and to read from it. This can be used to store game save data or player configuration files, for example. Here's a sample on how to write and read from a file: :: func save(content): var file = File.new() file.open("user://save_game.dat", file.WRITE) file.store_string(content) file.close() func load(): var file = File.new() file.open("user://save_game.dat", file.READ) var content = file.get_as_text() file.close() return content Member Function Description --------------------------- .. _class_File_close: - void **close** **(** **)** Close the currently opened file. .. _class_File_eof_reached: - :ref:`bool` **eof_reached** **(** **)** const Return whether the file cursor reached the end of the file. .. _class_File_file_exists: - :ref:`bool` **file_exists** **(** :ref:`String` path **)** const Get whether or not the file in the specified path exists. .. _class_File_get_16: - :ref:`int` **get_16** **(** **)** const Get the next 16 bits from the file as an integer. .. _class_File_get_32: - :ref:`int` **get_32** **(** **)** const Get the next 32 bits from the file as an integer. .. _class_File_get_64: - :ref:`int` **get_64** **(** **)** const Get the next 64 bits from the file as an integer. .. _class_File_get_8: - :ref:`int` **get_8** **(** **)** const Get the next 8 bits from the file as an integer. .. _class_File_get_as_text: - :ref:`String` **get_as_text** **(** **)** const Get the whole file as a :ref:`String`. .. _class_File_get_buffer: - :ref:`PoolByteArray` **get_buffer** **(** :ref:`int` len **)** const Get next len bytes of the file as a :ref:`PoolByteArray`. .. _class_File_get_csv_line: - :ref:`PoolStringArray` **get_csv_line** **(** :ref:`String` delim="," **)** const Get the next value of the file in CSV (Comma Separated Values) format. You can pass a different delimiter to use other than the default "," (comma). .. _class_File_get_double: - :ref:`float` **get_double** **(** **)** const Get the next 64 bits from the file as a floating point number. .. _class_File_get_endian_swap: - :ref:`bool` **get_endian_swap** **(** **)** Get whether endian swap is enabled for this file. .. _class_File_get_error: - :ref:`int` **get_error** **(** **)** const Get the last error that happened when trying to perform operations. Compare with the ``ERR_FILE\_\*`` constants from :ref:`@Global Scope`. .. _class_File_get_float: - :ref:`float` **get_float** **(** **)** const Get the next 32 bits from the file as a floating point number. .. _class_File_get_len: - :ref:`int` **get_len** **(** **)** const Return the size of the file in bytes. .. _class_File_get_line: - :ref:`String` **get_line** **(** **)** const Get the next line of the file as a :ref:`String`. .. _class_File_get_md5: - :ref:`String` **get_md5** **(** :ref:`String` path **)** const Return a md5 String representing the file at the given path or an empty :ref:`String` on failure. .. _class_File_get_modified_time: - :ref:`int` **get_modified_time** **(** :ref:`String` file **)** const .. _class_File_get_pascal_string: - :ref:`String` **get_pascal_string** **(** **)** Get a :ref:`String` saved in Pascal format from the file. .. _class_File_get_pos: - :ref:`int` **get_pos** **(** **)** const Return the file cursor position. .. _class_File_get_real: - :ref:`float` **get_real** **(** **)** const Get the next bits from the file as a floating point number. .. _class_File_get_sha256: - :ref:`String` **get_sha256** **(** :ref:`String` path **)** const Return a sha256 String representing the file at the given path or an empty :ref:`String` on failure. .. _class_File_get_var: - :ref:`Variant` **get_var** **(** **)** const Get the next Variant value from the file. .. _class_File_is_open: - :ref:`bool` **is_open** **(** **)** const Return whether the file is currently opened. .. _class_File_open: - :ref:`int` **open** **(** :ref:`String` path, :ref:`int` flags **)** Open the file for writing or reading, depending on the flags. .. _class_File_open_compressed: - :ref:`int` **open_compressed** **(** :ref:`String` path, :ref:`int` mode_flags, :ref:`int` compression_mode=null **)** Open a compressed file for reading or writing. The compression_mode can be set as one of the COMPRESSION\_\* constants. .. _class_File_open_encrypted: - :ref:`int` **open_encrypted** **(** :ref:`String` path, :ref:`int` mode_flags, :ref:`PoolByteArray` key **)** Open an encrypted file in write or read mode. You need to pass a binary key to encrypt/decrypt it. .. _class_File_open_encrypted_with_pass: - :ref:`int` **open_encrypted_with_pass** **(** :ref:`String` path, :ref:`int` mode_flags, :ref:`String` pass **)** Open an encrypted file in write or read mode. You need to pass a password to encrypt/decrypt it. .. _class_File_seek: - void **seek** **(** :ref:`int` pos **)** Change the file reading/writing cursor to the specified position (in bytes from the beginning of the file). .. _class_File_seek_end: - void **seek_end** **(** :ref:`int` pos=0 **)** Change the file reading/writing cursor to the specified position (in bytes from the end of the file). Note that this is an offset, so you should use negative numbers or the cursor will be at the end of the file. .. _class_File_set_endian_swap: - void **set_endian_swap** **(** :ref:`bool` enable **)** Set whether to swap the endianness of the file. Enable this if you're dealing with files written in big endian machines. Note that this is about the file format, not CPU type. This is always reseted to ``false`` whenever you open the file. .. _class_File_store_16: - void **store_16** **(** :ref:`int` value **)** Store an integer as 16 bits in the file. .. _class_File_store_32: - void **store_32** **(** :ref:`int` value **)** Store an integer as 32 bits in the file. .. _class_File_store_64: - void **store_64** **(** :ref:`int` value **)** Store an integer as 64 bits in the file. .. _class_File_store_8: - void **store_8** **(** :ref:`int` value **)** Store an integer as 8 bits in the file. .. _class_File_store_buffer: - void **store_buffer** **(** :ref:`PoolByteArray` buffer **)** Store the given array of bytes in the file. .. _class_File_store_double: - void **store_double** **(** :ref:`float` value **)** Store a floating point number as 64 bits in the file. .. _class_File_store_float: - void **store_float** **(** :ref:`float` value **)** Store a floating point number as 32 bits in the file. .. _class_File_store_line: - void **store_line** **(** :ref:`String` line **)** Store the given :ref:`String` as a line in the file. .. _class_File_store_pascal_string: - void **store_pascal_string** **(** :ref:`String` string **)** Store the given :ref:`String` as a line in the file in Pascal format (i.e. also store the length of the string). .. _class_File_store_real: - void **store_real** **(** :ref:`float` value **)** Store a floating point number in the file. .. _class_File_store_string: - void **store_string** **(** :ref:`String` string **)** Store the given :ref:`String` in the file. .. _class_File_store_var: - void **store_var** **(** :ref:`Variant` value **)** Store any Variant value in the file.