class_file.rst 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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_double<class_File_store_double>` **(** :ref:`float<class_float>` value **)** |
  93. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  94. | void | :ref:`store_float<class_File_store_float>` **(** :ref:`float<class_float>` value **)** |
  95. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  96. | void | :ref:`store_line<class_File_store_line>` **(** :ref:`String<class_String>` line **)** |
  97. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  98. | void | :ref:`store_pascal_string<class_File_store_pascal_string>` **(** :ref:`String<class_String>` string **)** |
  99. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  100. | void | :ref:`store_real<class_File_store_real>` **(** :ref:`float<class_float>` value **)** |
  101. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  102. | void | :ref:`store_string<class_File_store_string>` **(** :ref:`String<class_String>` string **)** |
  103. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  104. | void | :ref:`store_var<class_File_store_var>` **(** :ref:`Variant<class_Variant>` value **)** |
  105. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  106. Enumerations
  107. ------------
  108. .. _enum_File_CompressionMode:
  109. enum **CompressionMode**:
  110. - **COMPRESSION_FASTLZ** = **0** --- Uses the FastLZ compression method.
  111. - **COMPRESSION_DEFLATE** = **1** --- Uses the Deflate compression method.
  112. - **COMPRESSION_ZSTD** = **2** --- Uses the Zstd compression method.
  113. - **COMPRESSION_GZIP** = **3** --- Uses the gzip compression method.
  114. .. _enum_File_ModeFlags:
  115. enum **ModeFlags**:
  116. - **READ** = **1** --- Opens the file for read operations.
  117. - **WRITE** = **2** --- Opens the file for write operations. Create it if the file does not exist and truncate if it exists.
  118. - **READ_WRITE** = **3** --- Opens the file for read and write operations. Does not truncate the file.
  119. - **WRITE_READ** = **7** --- Opens the file for read and write operations. Create it if the file does not exist and truncate if it exists.
  120. Description
  121. -----------
  122. 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.
  123. Here's a sample on how to write and read from a file:
  124. ::
  125. func save(content):
  126. var file = File.new()
  127. file.open("user://save_game.dat", File.WRITE)
  128. file.store_string(content)
  129. file.close()
  130. func load():
  131. var file = File.new()
  132. file.open("user://save_game.dat", File.READ)
  133. var content = file.get_as_text()
  134. file.close()
  135. return content
  136. Tutorials
  137. ---------
  138. - :doc:`../getting_started/step_by_step/filesystem`
  139. Property Descriptions
  140. ---------------------
  141. .. _class_File_endian_swap:
  142. - :ref:`bool<class_bool>` **endian_swap**
  143. +----------+------------------------+
  144. | *Setter* | set_endian_swap(value) |
  145. +----------+------------------------+
  146. | *Getter* | get_endian_swap() |
  147. +----------+------------------------+
  148. If ``true`` the file's endianness is swapped. Use this if you're dealing with files written in big endian machines.
  149. Note that this is about the file format, not CPU type. This is always reset to ``false`` whenever you open the file.
  150. Method Descriptions
  151. -------------------
  152. .. _class_File_close:
  153. - void **close** **(** **)**
  154. Closes the currently opened file.
  155. .. _class_File_eof_reached:
  156. - :ref:`bool<class_bool>` **eof_reached** **(** **)** const
  157. Returns ``true`` if the file cursor has reached the end of the file.
  158. .. _class_File_file_exists:
  159. - :ref:`bool<class_bool>` **file_exists** **(** :ref:`String<class_String>` path **)** const
  160. Returns ``true`` if the file exists in the given path.
  161. .. _class_File_get_16:
  162. - :ref:`int<class_int>` **get_16** **(** **)** const
  163. Returns the next 16 bits from the file as an integer.
  164. .. _class_File_get_32:
  165. - :ref:`int<class_int>` **get_32** **(** **)** const
  166. Returns the next 32 bits from the file as an integer.
  167. .. _class_File_get_64:
  168. - :ref:`int<class_int>` **get_64** **(** **)** const
  169. Returns the next 64 bits from the file as an integer.
  170. .. _class_File_get_8:
  171. - :ref:`int<class_int>` **get_8** **(** **)** const
  172. Returns the next 8 bits from the file as an integer.
  173. .. _class_File_get_as_text:
  174. - :ref:`String<class_String>` **get_as_text** **(** **)** const
  175. Returns the whole file as a :ref:`String<class_String>`.
  176. .. _class_File_get_buffer:
  177. - :ref:`PoolByteArray<class_PoolByteArray>` **get_buffer** **(** :ref:`int<class_int>` len **)** const
  178. Returns next ``len`` bytes of the file as a :ref:`PoolByteArray<class_PoolByteArray>`.
  179. .. _class_File_get_csv_line:
  180. - :ref:`PoolStringArray<class_PoolStringArray>` **get_csv_line** **(** :ref:`String<class_String>` delim="," **)** const
  181. 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).
  182. .. _class_File_get_double:
  183. - :ref:`float<class_float>` **get_double** **(** **)** const
  184. Returns the next 64 bits from the file as a floating point number.
  185. .. _class_File_get_error:
  186. - :ref:`Error<enum_@GlobalScope_Error>` **get_error** **(** **)** const
  187. Returns the last error that happened when trying to perform operations. Compare with the ``ERR_FILE_*`` constants from :ref:`@GlobalScope<class_@GlobalScope>`.
  188. .. _class_File_get_float:
  189. - :ref:`float<class_float>` **get_float** **(** **)** const
  190. Returns the next 32 bits from the file as a floating point number.
  191. .. _class_File_get_len:
  192. - :ref:`int<class_int>` **get_len** **(** **)** const
  193. Returns the size of the file in bytes.
  194. .. _class_File_get_line:
  195. - :ref:`String<class_String>` **get_line** **(** **)** const
  196. Returns the next line of the file as a :ref:`String<class_String>`.
  197. .. _class_File_get_md5:
  198. - :ref:`String<class_String>` **get_md5** **(** :ref:`String<class_String>` path **)** const
  199. Returns an MD5 String representing the file at the given path or an empty :ref:`String<class_String>` on failure.
  200. .. _class_File_get_modified_time:
  201. - :ref:`int<class_int>` **get_modified_time** **(** :ref:`String<class_String>` file **)** const
  202. 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>`.
  203. .. _class_File_get_pascal_string:
  204. - :ref:`String<class_String>` **get_pascal_string** **(** **)**
  205. Returns a :ref:`String<class_String>` saved in Pascal format from the file.
  206. .. _class_File_get_path:
  207. - :ref:`String<class_String>` **get_path** **(** **)** const
  208. Returns the path as a :ref:`String<class_String>` for the current open file.
  209. .. _class_File_get_path_absolute:
  210. - :ref:`String<class_String>` **get_path_absolute** **(** **)** const
  211. Returns the absolute path as a :ref:`String<class_String>` for the current open file.
  212. .. _class_File_get_position:
  213. - :ref:`int<class_int>` **get_position** **(** **)** const
  214. Returns the file cursor's position.
  215. .. _class_File_get_real:
  216. - :ref:`float<class_float>` **get_real** **(** **)** const
  217. Returns the next bits from the file as a floating point number.
  218. .. _class_File_get_sha256:
  219. - :ref:`String<class_String>` **get_sha256** **(** :ref:`String<class_String>` path **)** const
  220. Returns a SHA-256 :ref:`String<class_String>` representing the file at the given path or an empty :ref:`String<class_String>` on failure.
  221. .. _class_File_get_var:
  222. - :ref:`Variant<class_Variant>` **get_var** **(** **)** const
  223. Returns the next :ref:`Variant<class_Variant>` value from the file.
  224. .. _class_File_is_open:
  225. - :ref:`bool<class_bool>` **is_open** **(** **)** const
  226. Returns ``true`` if the file is currently opened.
  227. .. _class_File_open:
  228. - :ref:`Error<enum_@GlobalScope_Error>` **open** **(** :ref:`String<class_String>` path, :ref:`int<class_int>` flags **)**
  229. Opens the file for writing or reading, depending on the flags.
  230. .. _class_File_open_compressed:
  231. - :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 **)**
  232. Opens a compressed file for reading or writing. Use COMPRESSION\_\* constants to set ``compression_mode``.
  233. .. _class_File_open_encrypted:
  234. - :ref:`Error<enum_@GlobalScope_Error>` **open_encrypted** **(** :ref:`String<class_String>` path, :ref:`int<class_int>` mode_flags, :ref:`PoolByteArray<class_PoolByteArray>` key **)**
  235. Opens an encrypted file in write or read mode. You need to pass a binary key to encrypt/decrypt it.
  236. .. _class_File_open_encrypted_with_pass:
  237. - :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 **)**
  238. Opens an encrypted file in write or read mode. You need to pass a password to encrypt/decrypt it.
  239. .. _class_File_seek:
  240. - void **seek** **(** :ref:`int<class_int>` position **)**
  241. Change the file reading/writing cursor to the specified position (in bytes from the beginning of the file).
  242. .. _class_File_seek_end:
  243. - void **seek_end** **(** :ref:`int<class_int>` position=0 **)**
  244. 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.
  245. .. _class_File_store_16:
  246. - void **store_16** **(** :ref:`int<class_int>` value **)**
  247. Stores an integer as 16 bits in the file.
  248. .. _class_File_store_32:
  249. - void **store_32** **(** :ref:`int<class_int>` value **)**
  250. Stores an integer as 32 bits in the file.
  251. .. _class_File_store_64:
  252. - void **store_64** **(** :ref:`int<class_int>` value **)**
  253. Stores an integer as 64 bits in the file.
  254. .. _class_File_store_8:
  255. - void **store_8** **(** :ref:`int<class_int>` value **)**
  256. Stores an integer as 8 bits in the file.
  257. .. _class_File_store_buffer:
  258. - void **store_buffer** **(** :ref:`PoolByteArray<class_PoolByteArray>` buffer **)**
  259. Stores the given array of bytes in the file.
  260. .. _class_File_store_double:
  261. - void **store_double** **(** :ref:`float<class_float>` value **)**
  262. Stores a floating point number as 64 bits in the file.
  263. .. _class_File_store_float:
  264. - void **store_float** **(** :ref:`float<class_float>` value **)**
  265. Stores a floating point number as 32 bits in the file.
  266. .. _class_File_store_line:
  267. - void **store_line** **(** :ref:`String<class_String>` line **)**
  268. Stores the given :ref:`String<class_String>` as a line in the file.
  269. .. _class_File_store_pascal_string:
  270. - void **store_pascal_string** **(** :ref:`String<class_String>` string **)**
  271. 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).
  272. .. _class_File_store_real:
  273. - void **store_real** **(** :ref:`float<class_float>` value **)**
  274. Stores a floating point number in the file.
  275. .. _class_File_store_string:
  276. - void **store_string** **(** :ref:`String<class_String>` string **)**
  277. Stores the given :ref:`String<class_String>` in the file.
  278. .. _class_File_store_var:
  279. - void **store_var** **(** :ref:`Variant<class_Variant>` value **)**
  280. Stores any Variant value in the file.