github_url: | hide |
---|
Inherits: :ref:`Reference<class_Reference>` < :ref:`Object<class_Object>`
Type to handle file reading and writing operations.
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
:ref:`bool<class_bool>` | :ref:`endian_swap<class_File_property_endian_swap>` | false |
enum ModeFlags:
- READ = 1 --- Opens the file for read operations.
- WRITE = 2 --- Opens the file for write operations. Create it if the file does not exist and truncate if it exists.
- READ_WRITE = 3 --- Opens the file for read and write operations. Does not truncate the file.
- WRITE_READ = 7 --- Opens the file for read and write operations. Create it if the file does not exist and truncate if it exists.
enum CompressionMode:
- COMPRESSION_FASTLZ = 0 --- Uses the FastLZ compression method.
- COMPRESSION_DEFLATE = 1 --- Uses the DEFLATE compression method.
- COMPRESSION_ZSTD = 2 --- Uses the Zstandard compression method.
- COMPRESSION_GZIP = 3 --- Uses the gzip compression method.
- :ref:`bool<class_bool>` endian_swap
Default | false |
Setter | set_endian_swap(value) |
Getter | get_endian_swap() |
If true
, the file's endianness is swapped. Use this if you're dealing with files written on big-endian machines.
Note: This is about the file format, not CPU type. This is always reset to false
whenever you open the file.
- void close ( )
Closes the currently opened file.
- :ref:`bool<class_bool>` eof_reached ( ) const
Returns true
if the file cursor has read past the end of the file.
Note: This function will still return false
while at the end of the file and only activates when reading past it. This can be confusing but it conforms to how low-level file access works in all operating systems. There is always :ref:`get_len<class_File_method_get_len>` and :ref:`get_position<class_File_method_get_position>` to implement a custom logic.
- :ref:`bool<class_bool>` file_exists ( :ref:`String<class_String>` path ) const
Returns true
if the file exists in the given path.
Note: Many resources types are imported (e.g. textures or sound files), and that their source asset will not be included in the exported game, as only the imported version is used (in the res://.import
folder). To check for the existence of such resources while taking into account the remapping to their imported location, use :ref:`ResourceLoader.exists<class_ResourceLoader_method_exists>`. Typically, using File.file_exists
on an imported resource would work while you are developing in the editor (the source asset is present in res://
, but fail when exported).
- :ref:`int<class_int>` get_16 ( ) const
Returns the next 16 bits from the file as an integer.
- :ref:`int<class_int>` get_32 ( ) const
Returns the next 32 bits from the file as an integer.
- :ref:`int<class_int>` get_64 ( ) const
Returns the next 64 bits from the file as an integer.
- :ref:`int<class_int>` get_8 ( ) const
Returns the next 8 bits from the file as an integer.
- :ref:`String<class_String>` get_as_text ( ) const
Returns the whole file as a :ref:`String<class_String>`.
Text is interpreted as being UTF-8 encoded.
- :ref:`PackedByteArray<class_PackedByteArray>` get_buffer ( :ref:`int<class_int>` len ) const
Returns next len
bytes of the file as a :ref:`PackedByteArray<class_PackedByteArray>`.
- :ref:`PackedStringArray<class_PackedStringArray>` get_csv_line ( :ref:`String<class_String>` delim="," ) const
Returns the next value of the file in CSV (Comma-Separated Values) format. You can pass a different delimiter delim
to use other than the default ","
(comma). This delimiter must be one-character long.
Text is interpreted as being UTF-8 encoded.
- :ref:`float<class_float>` get_double ( ) const
Returns the next 64 bits from the file as a floating-point number.
- :ref:`Error<enum_@GlobalScope_Error>` get_error ( ) const
Returns the last error that happened when trying to perform operations. Compare with the ERR_FILE_*
constants from :ref:`Error<enum_@GlobalScope_Error>`.
- :ref:`float<class_float>` get_float ( ) const
Returns the next 32 bits from the file as a floating-point number.
- :ref:`int<class_int>` get_len ( ) const
Returns the size of the file in bytes.
- :ref:`String<class_String>` get_line ( ) const
Returns the next line of the file as a :ref:`String<class_String>`.
Text is interpreted as being UTF-8 encoded.
- :ref:`String<class_String>` get_md5 ( :ref:`String<class_String>` path ) const
Returns an MD5 String representing the file at the given path or an empty :ref:`String<class_String>` on failure.
- :ref:`int<class_int>` get_modified_time ( :ref:`String<class_String>` file ) const
Returns the last time the file
was modified in unix timestamp format or returns a :ref:`String<class_String>` "ERROR IN file
". This unix timestamp can be converted to datetime by using :ref:`OS.get_datetime_from_unix_time<class_OS_method_get_datetime_from_unix_time>`.
- :ref:`String<class_String>` get_pascal_string ( )
Returns a :ref:`String<class_String>` saved in Pascal format from the file.
Text is interpreted as being UTF-8 encoded.
- :ref:`String<class_String>` get_path ( ) const
Returns the path as a :ref:`String<class_String>` for the current open file.
- :ref:`String<class_String>` get_path_absolute ( ) const
Returns the absolute path as a :ref:`String<class_String>` for the current open file.
- :ref:`int<class_int>` get_position ( ) const
Returns the file cursor's position.
- :ref:`float<class_float>` get_real ( ) const
Returns the next bits from the file as a floating-point number.
- :ref:`String<class_String>` get_sha256 ( :ref:`String<class_String>` path ) const
Returns a SHA-256 :ref:`String<class_String>` representing the file at the given path or an empty :ref:`String<class_String>` on failure.
- :ref:`Variant<class_Variant>` get_var ( :ref:`bool<class_bool>` allow_objects=false ) const
Returns the next :ref:`Variant<class_Variant>` value from the file. If allow_objects
is true
, decoding objects is allowed.
Warning: Deserialized objects can contain code which gets executed. Do not use this option if the serialized object comes from untrusted sources to avoid potential security threats such as remote code execution.
- :ref:`bool<class_bool>` is_open ( ) const
Returns true
if the file is currently opened.
- :ref:`Error<enum_@GlobalScope_Error>` open ( :ref:`String<class_String>` path, :ref:`ModeFlags<enum_File_ModeFlags>` flags )
Opens the file for writing or reading, depending on the flags.
- :ref:`Error<enum_@GlobalScope_Error>` open_compressed ( :ref:`String<class_String>` path, :ref:`ModeFlags<enum_File_ModeFlags>` mode_flags, :ref:`CompressionMode<enum_File_CompressionMode>` compression_mode=0 )
Opens a compressed file for reading or writing.
- :ref:`Error<enum_@GlobalScope_Error>` open_encrypted ( :ref:`String<class_String>` path, :ref:`ModeFlags<enum_File_ModeFlags>` mode_flags, :ref:`PackedByteArray<class_PackedByteArray>` key )
Opens an encrypted file in write or read mode. You need to pass a binary key to encrypt/decrypt it.
- :ref:`Error<enum_@GlobalScope_Error>` open_encrypted_with_pass ( :ref:`String<class_String>` path, :ref:`ModeFlags<enum_File_ModeFlags>` mode_flags, :ref:`String<class_String>` pass )
Opens an encrypted file in write or read mode. You need to pass a password to encrypt/decrypt it.
- void seek ( :ref:`int<class_int>` position )
Changes the file reading/writing cursor to the specified position (in bytes from the beginning of the file).
- void seek_end ( :ref:`int<class_int>` position=0 )
Changes the file reading/writing cursor to the specified position (in bytes from the end of the file).
Note: This is an offset, so you should use negative numbers or the cursor will be at the end of the file.
- void store_16 ( :ref:`int<class_int>` value )
Stores an integer as 16 bits in the file.
- void store_32 ( :ref:`int<class_int>` value )
Stores an integer as 32 bits in the file.
- void store_64 ( :ref:`int<class_int>` value )
Stores an integer as 64 bits in the file.
- void store_8 ( :ref:`int<class_int>` value )
Stores an integer as 8 bits in the file.
- void store_buffer ( :ref:`PackedByteArray<class_PackedByteArray>` buffer )
Stores the given array of bytes in the file.
- void store_csv_line ( :ref:`PackedStringArray<class_PackedStringArray>` values, :ref:`String<class_String>` delim="," )
Store the given :ref:`PackedStringArray<class_PackedStringArray>` in the file as a line formatted in the CSV (Comma-Separated Values) format. You can pass a different delimiter delim
to use other than the default ","
(comma). This delimiter must be one-character long.
Text will be encoded as UTF-8.
- void store_double ( :ref:`float<class_float>` value )
Stores a floating-point number as 64 bits in the file.
- void store_float ( :ref:`float<class_float>` value )
Stores a floating-point number as 32 bits in the file.
- void store_line ( :ref:`String<class_String>` line )
Stores the given :ref:`String<class_String>` as a line in the file.
Text will be encoded as UTF-8.
- void store_pascal_string ( :ref:`String<class_String>` string )
Stores the given :ref:`String<class_String>` as a line in the file in Pascal format (i.e. also store the length of the string).
Text will be encoded as UTF-8.
- void store_real ( :ref:`float<class_float>` value )
Stores a floating-point number in the file.
- void store_string ( :ref:`String<class_String>` string )
Stores the given :ref:`String<class_String>` in the file.
Text will be encoded as UTF-8.
- void store_var ( :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` full_objects=false )
Stores any Variant value in the file. If full_objects
is true
, encoding objects is allowed (and can potentially include code).