class_file.rst 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. .. Generated automatically by doc/tools/makerst.py in Godot's source tree.
  2. .. DO NOT EDIT THIS FILE, but the File.xml source instead.
  3. .. The source is found in doc/classes or modules/<name>/doc_classes.
  4. .. _class_File:
  5. File
  6. ====
  7. **Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  8. **Category:** Core
  9. Brief Description
  10. -----------------
  11. Type to handle file reading and writing operations.
  12. Properties
  13. ----------
  14. +-------------------------+--------------------------------------------+
  15. | :ref:`bool<class_bool>` | :ref:`endian_swap<class_File_endian_swap>` |
  16. +-------------------------+--------------------------------------------+
  17. Methods
  18. -------
  19. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  20. | void | :ref:`close<class_File_close>` **(** **)** |
  21. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  22. | :ref:`bool<class_bool>` | :ref:`eof_reached<class_File_eof_reached>` **(** **)** const |
  23. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  24. | :ref:`bool<class_bool>` | :ref:`file_exists<class_File_file_exists>` **(** :ref:`String<class_String>` path **)** const |
  25. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  26. | :ref:`int<class_int>` | :ref:`get_16<class_File_get_16>` **(** **)** const |
  27. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  28. | :ref:`int<class_int>` | :ref:`get_32<class_File_get_32>` **(** **)** const |
  29. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  30. | :ref:`int<class_int>` | :ref:`get_64<class_File_get_64>` **(** **)** const |
  31. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  32. | :ref:`int<class_int>` | :ref:`get_8<class_File_get_8>` **(** **)** const |
  33. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  34. | :ref:`String<class_String>` | :ref:`get_as_text<class_File_get_as_text>` **(** **)** const |
  35. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  36. | :ref:`PoolByteArray<class_PoolByteArray>` | :ref:`get_buffer<class_File_get_buffer>` **(** :ref:`int<class_int>` len **)** const |
  37. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  38. | :ref:`PoolStringArray<class_PoolStringArray>` | :ref:`get_csv_line<class_File_get_csv_line>` **(** :ref:`String<class_String>` delim="," **)** const |
  39. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  40. | :ref:`float<class_float>` | :ref:`get_double<class_File_get_double>` **(** **)** const |
  41. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  42. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`get_error<class_File_get_error>` **(** **)** const |
  43. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | :ref:`float<class_float>` | :ref:`get_float<class_File_get_float>` **(** **)** const |
  45. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  46. | :ref:`int<class_int>` | :ref:`get_len<class_File_get_len>` **(** **)** const |
  47. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  48. | :ref:`String<class_String>` | :ref:`get_line<class_File_get_line>` **(** **)** const |
  49. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | :ref:`String<class_String>` | :ref:`get_md5<class_File_get_md5>` **(** :ref:`String<class_String>` path **)** const |
  51. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | :ref:`int<class_int>` | :ref:`get_modified_time<class_File_get_modified_time>` **(** :ref:`String<class_String>` file **)** const |
  53. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | :ref:`String<class_String>` | :ref:`get_pascal_string<class_File_get_pascal_string>` **(** **)** |
  55. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | :ref:`String<class_String>` | :ref:`get_path<class_File_get_path>` **(** **)** const |
  57. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | :ref:`String<class_String>` | :ref:`get_path_absolute<class_File_get_path_absolute>` **(** **)** const |
  59. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | :ref:`int<class_int>` | :ref:`get_position<class_File_get_position>` **(** **)** const |
  61. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | :ref:`float<class_float>` | :ref:`get_real<class_File_get_real>` **(** **)** const |
  63. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | :ref:`String<class_String>` | :ref:`get_sha256<class_File_get_sha256>` **(** :ref:`String<class_String>` path **)** const |
  65. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | :ref:`Variant<class_Variant>` | :ref:`get_var<class_File_get_var>` **(** **)** const |
  67. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | :ref:`bool<class_bool>` | :ref:`is_open<class_File_is_open>` **(** **)** const |
  69. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  70. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`open<class_File_open>` **(** :ref:`String<class_String>` path, :ref:`int<class_int>` flags **)** |
  71. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  72. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`open_compressed<class_File_open_compressed>` **(** :ref:`String<class_String>` path, :ref:`int<class_int>` mode_flags, :ref:`int<class_int>` compression_mode=0 **)** |
  73. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  74. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`open_encrypted<class_File_open_encrypted>` **(** :ref:`String<class_String>` path, :ref:`int<class_int>` mode_flags, :ref:`PoolByteArray<class_PoolByteArray>` key **)** |
  75. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  76. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`open_encrypted_with_pass<class_File_open_encrypted_with_pass>` **(** :ref:`String<class_String>` path, :ref:`int<class_int>` mode_flags, :ref:`String<class_String>` pass **)** |
  77. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  78. | void | :ref:`seek<class_File_seek>` **(** :ref:`int<class_int>` position **)** |
  79. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  80. | void | :ref:`seek_end<class_File_seek_end>` **(** :ref:`int<class_int>` position=0 **)** |
  81. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  82. | void | :ref:`store_16<class_File_store_16>` **(** :ref:`int<class_int>` value **)** |
  83. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  84. | void | :ref:`store_32<class_File_store_32>` **(** :ref:`int<class_int>` value **)** |
  85. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  86. | void | :ref:`store_64<class_File_store_64>` **(** :ref:`int<class_int>` value **)** |
  87. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  88. | void | :ref:`store_8<class_File_store_8>` **(** :ref:`int<class_int>` value **)** |
  89. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  90. | void | :ref:`store_buffer<class_File_store_buffer>` **(** :ref:`PoolByteArray<class_PoolByteArray>` buffer **)** |
  91. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  92. | void | :ref:`store_csv_line<class_File_store_csv_line>` **(** :ref:`PoolStringArray<class_PoolStringArray>` values, :ref:`String<class_String>` delim="," **)** |
  93. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  94. | void | :ref:`store_double<class_File_store_double>` **(** :ref:`float<class_float>` value **)** |
  95. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  96. | void | :ref:`store_float<class_File_store_float>` **(** :ref:`float<class_float>` value **)** |
  97. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  98. | void | :ref:`store_line<class_File_store_line>` **(** :ref:`String<class_String>` line **)** |
  99. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  100. | void | :ref:`store_pascal_string<class_File_store_pascal_string>` **(** :ref:`String<class_String>` string **)** |
  101. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  102. | void | :ref:`store_real<class_File_store_real>` **(** :ref:`float<class_float>` value **)** |
  103. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  104. | void | :ref:`store_string<class_File_store_string>` **(** :ref:`String<class_String>` string **)** |
  105. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  106. | void | :ref:`store_var<class_File_store_var>` **(** :ref:`Variant<class_Variant>` value **)** |
  107. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  108. Enumerations
  109. ------------
  110. .. _enum_File_CompressionMode:
  111. enum **CompressionMode**:
  112. - **COMPRESSION_FASTLZ** = **0** --- Uses the FastLZ compression method.
  113. - **COMPRESSION_DEFLATE** = **1** --- Uses the Deflate compression method.
  114. - **COMPRESSION_ZSTD** = **2** --- Uses the Zstd compression method.
  115. - **COMPRESSION_GZIP** = **3** --- Uses the gzip compression method.
  116. .. _enum_File_ModeFlags:
  117. enum **ModeFlags**:
  118. - **READ** = **1** --- Opens the file for read operations.
  119. - **WRITE** = **2** --- Opens the file for write operations. Create it if the file does not exist and truncate if it exists.
  120. - **READ_WRITE** = **3** --- Opens the file for read and write operations. Does not truncate the file.
  121. - **WRITE_READ** = **7** --- Opens the file for read and write operations. Create it if the file does not exist and truncate if it exists.
  122. Description
  123. -----------
  124. 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.
  125. Here's a sample on how to write and read from a file:
  126. ::
  127. func save(content):
  128. var file = File.new()
  129. file.open("user://save_game.dat", File.WRITE)
  130. file.store_string(content)
  131. file.close()
  132. func load():
  133. var file = File.new()
  134. file.open("user://save_game.dat", File.READ)
  135. var content = file.get_as_text()
  136. file.close()
  137. return content
  138. Tutorials
  139. ---------
  140. - :doc:`../getting_started/step_by_step/filesystem`
  141. Property Descriptions
  142. ---------------------
  143. .. _class_File_endian_swap:
  144. - :ref:`bool<class_bool>` **endian_swap**
  145. +----------+------------------------+
  146. | *Setter* | set_endian_swap(value) |
  147. +----------+------------------------+
  148. | *Getter* | get_endian_swap() |
  149. +----------+------------------------+
  150. If ``true`` the file's endianness is swapped. Use this if you're dealing with files written in big endian machines.
  151. Note that this is about the file format, not CPU type. This is always reset to ``false`` whenever you open the file.
  152. Method Descriptions
  153. -------------------
  154. .. _class_File_close:
  155. - void **close** **(** **)**
  156. Closes the currently opened file.
  157. .. _class_File_eof_reached:
  158. - :ref:`bool<class_bool>` **eof_reached** **(** **)** const
  159. Returns ``true`` if the file cursor has reached the end of the file.
  160. .. _class_File_file_exists:
  161. - :ref:`bool<class_bool>` **file_exists** **(** :ref:`String<class_String>` path **)** const
  162. Returns ``true`` if the file exists in the given path.
  163. .. _class_File_get_16:
  164. - :ref:`int<class_int>` **get_16** **(** **)** const
  165. Returns the next 16 bits from the file as an integer.
  166. .. _class_File_get_32:
  167. - :ref:`int<class_int>` **get_32** **(** **)** const
  168. Returns the next 32 bits from the file as an integer.
  169. .. _class_File_get_64:
  170. - :ref:`int<class_int>` **get_64** **(** **)** const
  171. Returns the next 64 bits from the file as an integer.
  172. .. _class_File_get_8:
  173. - :ref:`int<class_int>` **get_8** **(** **)** const
  174. Returns the next 8 bits from the file as an integer.
  175. .. _class_File_get_as_text:
  176. - :ref:`String<class_String>` **get_as_text** **(** **)** const
  177. Returns the whole file as a :ref:`String<class_String>`.
  178. .. _class_File_get_buffer:
  179. - :ref:`PoolByteArray<class_PoolByteArray>` **get_buffer** **(** :ref:`int<class_int>` len **)** const
  180. Returns next ``len`` bytes of the file as a :ref:`PoolByteArray<class_PoolByteArray>`.
  181. .. _class_File_get_csv_line:
  182. - :ref:`PoolStringArray<class_PoolStringArray>` **get_csv_line** **(** :ref:`String<class_String>` delim="," **)** const
  183. Returns 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), it should be one character long.
  184. .. _class_File_get_double:
  185. - :ref:`float<class_float>` **get_double** **(** **)** const
  186. Returns the next 64 bits from the file as a floating point number.
  187. .. _class_File_get_error:
  188. - :ref:`Error<enum_@GlobalScope_Error>` **get_error** **(** **)** const
  189. Returns the last error that happened when trying to perform operations. Compare with the ``ERR_FILE_*`` constants from :ref:`@GlobalScope<class_@GlobalScope>`.
  190. .. _class_File_get_float:
  191. - :ref:`float<class_float>` **get_float** **(** **)** const
  192. Returns the next 32 bits from the file as a floating point number.
  193. .. _class_File_get_len:
  194. - :ref:`int<class_int>` **get_len** **(** **)** const
  195. Returns the size of the file in bytes.
  196. .. _class_File_get_line:
  197. - :ref:`String<class_String>` **get_line** **(** **)** const
  198. Returns the next line of the file as a :ref:`String<class_String>`.
  199. .. _class_File_get_md5:
  200. - :ref:`String<class_String>` **get_md5** **(** :ref:`String<class_String>` path **)** const
  201. Returns an MD5 String representing the file at the given path or an empty :ref:`String<class_String>` on failure.
  202. .. _class_File_get_modified_time:
  203. - :ref:`int<class_int>` **get_modified_time** **(** :ref:`String<class_String>` file **)** const
  204. 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_get_datetime_from_unix_time>`.
  205. .. _class_File_get_pascal_string:
  206. - :ref:`String<class_String>` **get_pascal_string** **(** **)**
  207. Returns a :ref:`String<class_String>` saved in Pascal format from the file.
  208. .. _class_File_get_path:
  209. - :ref:`String<class_String>` **get_path** **(** **)** const
  210. Returns the path as a :ref:`String<class_String>` for the current open file.
  211. .. _class_File_get_path_absolute:
  212. - :ref:`String<class_String>` **get_path_absolute** **(** **)** const
  213. Returns the absolute path as a :ref:`String<class_String>` for the current open file.
  214. .. _class_File_get_position:
  215. - :ref:`int<class_int>` **get_position** **(** **)** const
  216. Returns the file cursor's position.
  217. .. _class_File_get_real:
  218. - :ref:`float<class_float>` **get_real** **(** **)** const
  219. Returns the next bits from the file as a floating point number.
  220. .. _class_File_get_sha256:
  221. - :ref:`String<class_String>` **get_sha256** **(** :ref:`String<class_String>` path **)** const
  222. Returns a SHA-256 :ref:`String<class_String>` representing the file at the given path or an empty :ref:`String<class_String>` on failure.
  223. .. _class_File_get_var:
  224. - :ref:`Variant<class_Variant>` **get_var** **(** **)** const
  225. Returns the next :ref:`Variant<class_Variant>` value from the file.
  226. .. _class_File_is_open:
  227. - :ref:`bool<class_bool>` **is_open** **(** **)** const
  228. Returns ``true`` if the file is currently opened.
  229. .. _class_File_open:
  230. - :ref:`Error<enum_@GlobalScope_Error>` **open** **(** :ref:`String<class_String>` path, :ref:`int<class_int>` flags **)**
  231. Opens the file for writing or reading, depending on the flags.
  232. .. _class_File_open_compressed:
  233. - :ref:`Error<enum_@GlobalScope_Error>` **open_compressed** **(** :ref:`String<class_String>` path, :ref:`int<class_int>` mode_flags, :ref:`int<class_int>` compression_mode=0 **)**
  234. Opens a compressed file for reading or writing. Use COMPRESSION\_\* constants to set ``compression_mode``.
  235. .. _class_File_open_encrypted:
  236. - :ref:`Error<enum_@GlobalScope_Error>` **open_encrypted** **(** :ref:`String<class_String>` path, :ref:`int<class_int>` mode_flags, :ref:`PoolByteArray<class_PoolByteArray>` key **)**
  237. Opens an encrypted file in write or read mode. You need to pass a binary key to encrypt/decrypt it.
  238. .. _class_File_open_encrypted_with_pass:
  239. - :ref:`Error<enum_@GlobalScope_Error>` **open_encrypted_with_pass** **(** :ref:`String<class_String>` path, :ref:`int<class_int>` mode_flags, :ref:`String<class_String>` pass **)**
  240. Opens an encrypted file in write or read mode. You need to pass a password to encrypt/decrypt it.
  241. .. _class_File_seek:
  242. - void **seek** **(** :ref:`int<class_int>` position **)**
  243. Change the file reading/writing cursor to the specified position (in bytes from the beginning of the file).
  244. .. _class_File_seek_end:
  245. - void **seek_end** **(** :ref:`int<class_int>` position=0 **)**
  246. Changes 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.
  247. .. _class_File_store_16:
  248. - void **store_16** **(** :ref:`int<class_int>` value **)**
  249. Stores an integer as 16 bits in the file.
  250. .. _class_File_store_32:
  251. - void **store_32** **(** :ref:`int<class_int>` value **)**
  252. Stores an integer as 32 bits in the file.
  253. .. _class_File_store_64:
  254. - void **store_64** **(** :ref:`int<class_int>` value **)**
  255. Stores an integer as 64 bits in the file.
  256. .. _class_File_store_8:
  257. - void **store_8** **(** :ref:`int<class_int>` value **)**
  258. Stores an integer as 8 bits in the file.
  259. .. _class_File_store_buffer:
  260. - void **store_buffer** **(** :ref:`PoolByteArray<class_PoolByteArray>` buffer **)**
  261. Stores the given array of bytes in the file.
  262. .. _class_File_store_csv_line:
  263. - void **store_csv_line** **(** :ref:`PoolStringArray<class_PoolStringArray>` values, :ref:`String<class_String>` delim="," **)**
  264. Store the given :ref:`PoolStringArray<class_PoolStringArray>` in the file as a line formatted in the CSV (Comma Separated Values) format. You can pass a different delimiter to use other than the default "," (comma), it should be one character long.
  265. .. _class_File_store_double:
  266. - void **store_double** **(** :ref:`float<class_float>` value **)**
  267. Stores a floating point number as 64 bits in the file.
  268. .. _class_File_store_float:
  269. - void **store_float** **(** :ref:`float<class_float>` value **)**
  270. Stores a floating point number as 32 bits in the file.
  271. .. _class_File_store_line:
  272. - void **store_line** **(** :ref:`String<class_String>` line **)**
  273. Stores the given :ref:`String<class_String>` as a line in the file.
  274. .. _class_File_store_pascal_string:
  275. - void **store_pascal_string** **(** :ref:`String<class_String>` string **)**
  276. 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).
  277. .. _class_File_store_real:
  278. - void **store_real** **(** :ref:`float<class_float>` value **)**
  279. Stores a floating point number in the file.
  280. .. _class_File_store_string:
  281. - void **store_string** **(** :ref:`String<class_String>` string **)**
  282. Stores the given :ref:`String<class_String>` in the file.
  283. .. _class_File_store_var:
  284. - void **store_var** **(** :ref:`Variant<class_Variant>` value **)**
  285. Stores any Variant value in the file.