class_file.rst 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. Member Functions
  13. ----------------
  14. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  15. | void | :ref:`close<class_File_close>` **(** **)** |
  16. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  17. | :ref:`bool<class_bool>` | :ref:`eof_reached<class_File_eof_reached>` **(** **)** const |
  18. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  19. | :ref:`bool<class_bool>` | :ref:`file_exists<class_File_file_exists>` **(** :ref:`String<class_string>` path **)** const |
  20. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  21. | :ref:`int<class_int>` | :ref:`get_16<class_File_get_16>` **(** **)** const |
  22. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  23. | :ref:`int<class_int>` | :ref:`get_32<class_File_get_32>` **(** **)** const |
  24. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  25. | :ref:`int<class_int>` | :ref:`get_64<class_File_get_64>` **(** **)** const |
  26. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  27. | :ref:`int<class_int>` | :ref:`get_8<class_File_get_8>` **(** **)** const |
  28. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  29. | :ref:`String<class_string>` | :ref:`get_as_text<class_File_get_as_text>` **(** **)** const |
  30. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  31. | :ref:`PoolByteArray<class_poolbytearray>` | :ref:`get_buffer<class_File_get_buffer>` **(** :ref:`int<class_int>` len **)** const |
  32. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  33. | :ref:`PoolStringArray<class_poolstringarray>` | :ref:`get_csv_line<class_File_get_csv_line>` **(** :ref:`String<class_string>` delim="," **)** const |
  34. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  35. | :ref:`float<class_float>` | :ref:`get_double<class_File_get_double>` **(** **)** const |
  36. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  37. | :ref:`bool<class_bool>` | :ref:`get_endian_swap<class_File_get_endian_swap>` **(** **)** |
  38. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  39. | :ref:`int<class_int>` | :ref:`get_error<class_File_get_error>` **(** **)** const |
  40. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. | :ref:`float<class_float>` | :ref:`get_float<class_File_get_float>` **(** **)** const |
  42. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. | :ref:`int<class_int>` | :ref:`get_len<class_File_get_len>` **(** **)** const |
  44. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  45. | :ref:`String<class_string>` | :ref:`get_line<class_File_get_line>` **(** **)** const |
  46. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | :ref:`String<class_string>` | :ref:`get_md5<class_File_get_md5>` **(** :ref:`String<class_string>` path **)** const |
  48. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | :ref:`int<class_int>` | :ref:`get_modified_time<class_File_get_modified_time>` **(** :ref:`String<class_string>` file **)** const |
  50. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`String<class_string>` | :ref:`get_pascal_string<class_File_get_pascal_string>` **(** **)** |
  52. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | :ref:`int<class_int>` | :ref:`get_position<class_File_get_position>` **(** **)** const |
  54. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | :ref:`float<class_float>` | :ref:`get_real<class_File_get_real>` **(** **)** const |
  56. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | :ref:`String<class_string>` | :ref:`get_sha256<class_File_get_sha256>` **(** :ref:`String<class_string>` path **)** const |
  58. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  59. | :ref:`Variant<class_variant>` | :ref:`get_var<class_File_get_var>` **(** **)** const |
  60. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  61. | :ref:`bool<class_bool>` | :ref:`is_open<class_File_is_open>` **(** **)** const |
  62. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  63. | :ref:`int<class_int>` | :ref:`open<class_File_open>` **(** :ref:`String<class_string>` path, :ref:`int<class_int>` flags **)** |
  64. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`int<class_int>` | :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 **)** |
  66. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`int<class_int>` | :ref:`open_encrypted<class_File_open_encrypted>` **(** :ref:`String<class_string>` path, :ref:`int<class_int>` mode_flags, :ref:`PoolByteArray<class_poolbytearray>` key **)** |
  68. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  69. | :ref:`int<class_int>` | :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 **)** |
  70. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  71. | void | :ref:`seek<class_File_seek>` **(** :ref:`int<class_int>` position **)** |
  72. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  73. | void | :ref:`seek_end<class_File_seek_end>` **(** :ref:`int<class_int>` position=0 **)** |
  74. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  75. | void | :ref:`set_endian_swap<class_File_set_endian_swap>` **(** :ref:`bool<class_bool>` enable **)** |
  76. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  77. | void | :ref:`store_16<class_File_store_16>` **(** :ref:`int<class_int>` value **)** |
  78. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  79. | void | :ref:`store_32<class_File_store_32>` **(** :ref:`int<class_int>` value **)** |
  80. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  81. | void | :ref:`store_64<class_File_store_64>` **(** :ref:`int<class_int>` value **)** |
  82. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  83. | void | :ref:`store_8<class_File_store_8>` **(** :ref:`int<class_int>` value **)** |
  84. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  85. | void | :ref:`store_buffer<class_File_store_buffer>` **(** :ref:`PoolByteArray<class_poolbytearray>` buffer **)** |
  86. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  87. | void | :ref:`store_double<class_File_store_double>` **(** :ref:`float<class_float>` value **)** |
  88. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  89. | void | :ref:`store_float<class_File_store_float>` **(** :ref:`float<class_float>` value **)** |
  90. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  91. | void | :ref:`store_line<class_File_store_line>` **(** :ref:`String<class_string>` line **)** |
  92. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  93. | void | :ref:`store_pascal_string<class_File_store_pascal_string>` **(** :ref:`String<class_string>` string **)** |
  94. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  95. | void | :ref:`store_real<class_File_store_real>` **(** :ref:`float<class_float>` value **)** |
  96. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  97. | void | :ref:`store_string<class_File_store_string>` **(** :ref:`String<class_string>` string **)** |
  98. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  99. | void | :ref:`store_var<class_File_store_var>` **(** :ref:`Variant<class_variant>` value **)** |
  100. +------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  101. Enums
  102. -----
  103. .. _enum_File_CompressionMode:
  104. enum **CompressionMode**
  105. - **COMPRESSION_FASTLZ** = **0** --- Uses the FastLZ compression method.
  106. - **COMPRESSION_DEFLATE** = **1** --- Uses the Deflate compression method.
  107. - **COMPRESSION_ZSTD** = **2** --- Uses the Zstd compression method.
  108. - **COMPRESSION_GZIP** = **3** --- Uses the gzip compression method.
  109. .. _enum_File_ModeFlags:
  110. enum **ModeFlags**
  111. - **READ** = **1** --- Opens the file for read operations.
  112. - **WRITE** = **2** --- Opens the file for write operations. Create it if the file does not exist and truncate if it exists.
  113. - **READ_WRITE** = **3** --- Opens the file for read and write operations. Does not truncate the file.
  114. - **WRITE_READ** = **7** --- Opens the file for read and write operations. Create it if the file does not exist and truncate if it exists.
  115. Description
  116. -----------
  117. 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.
  118. Here's a sample on how to write and read from a file:
  119. ::
  120. func save(content):
  121. var file = File.new()
  122. file.open("user://save_game.dat", file.WRITE)
  123. file.store_string(content)
  124. file.close()
  125. func load():
  126. var file = File.new()
  127. file.open("user://save_game.dat", file.READ)
  128. var content = file.get_as_text()
  129. file.close()
  130. return content
  131. Member Function Description
  132. ---------------------------
  133. .. _class_File_close:
  134. - void **close** **(** **)**
  135. Closes the currently opened file.
  136. .. _class_File_eof_reached:
  137. - :ref:`bool<class_bool>` **eof_reached** **(** **)** const
  138. Returns ``true`` if the file cursor has reached the end of the file.
  139. .. _class_File_file_exists:
  140. - :ref:`bool<class_bool>` **file_exists** **(** :ref:`String<class_string>` path **)** const
  141. Returns ``true`` if the file exists in the given path.
  142. .. _class_File_get_16:
  143. - :ref:`int<class_int>` **get_16** **(** **)** const
  144. Returns the next 16 bits from the file as an integer.
  145. .. _class_File_get_32:
  146. - :ref:`int<class_int>` **get_32** **(** **)** const
  147. Returns the next 32 bits from the file as an integer.
  148. .. _class_File_get_64:
  149. - :ref:`int<class_int>` **get_64** **(** **)** const
  150. Returns the next 64 bits from the file as an integer.
  151. .. _class_File_get_8:
  152. - :ref:`int<class_int>` **get_8** **(** **)** const
  153. Returns the next 8 bits from the file as an integer.
  154. .. _class_File_get_as_text:
  155. - :ref:`String<class_string>` **get_as_text** **(** **)** const
  156. Returns the whole file as a :ref:`String<class_string>`.
  157. .. _class_File_get_buffer:
  158. - :ref:`PoolByteArray<class_poolbytearray>` **get_buffer** **(** :ref:`int<class_int>` len **)** const
  159. Returns next ``len`` bytes of the file as a :ref:`PoolByteArray<class_poolbytearray>`.
  160. .. _class_File_get_csv_line:
  161. - :ref:`PoolStringArray<class_poolstringarray>` **get_csv_line** **(** :ref:`String<class_string>` delim="," **)** const
  162. 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).
  163. .. _class_File_get_double:
  164. - :ref:`float<class_float>` **get_double** **(** **)** const
  165. Returns the next 64 bits from the file as a floating point number.
  166. .. _class_File_get_endian_swap:
  167. - :ref:`bool<class_bool>` **get_endian_swap** **(** **)**
  168. Returns ``true`` if endian swap is enabled for this file.
  169. .. _class_File_get_error:
  170. - :ref:`int<class_int>` **get_error** **(** **)** const
  171. Returns the last error that happened when trying to perform operations. Compare with the ``ERR_FILE\_\*`` constants from :ref:`@GlobalScope<class_@globalscope>`.
  172. .. _class_File_get_float:
  173. - :ref:`float<class_float>` **get_float** **(** **)** const
  174. Returns the next 32 bits from the file as a floating point number.
  175. .. _class_File_get_len:
  176. - :ref:`int<class_int>` **get_len** **(** **)** const
  177. Returns the size of the file in bytes.
  178. .. _class_File_get_line:
  179. - :ref:`String<class_string>` **get_line** **(** **)** const
  180. Returns the next line of the file as a :ref:`String<class_string>`.
  181. .. _class_File_get_md5:
  182. - :ref:`String<class_string>` **get_md5** **(** :ref:`String<class_string>` path **)** const
  183. Returns an MD5 String representing the file at the given path or an empty :ref:`String<class_string>` on failure.
  184. .. _class_File_get_modified_time:
  185. - :ref:`int<class_int>` **get_modified_time** **(** :ref:`String<class_string>` file **)** const
  186. 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>`.
  187. .. _class_File_get_pascal_string:
  188. - :ref:`String<class_string>` **get_pascal_string** **(** **)**
  189. Returns a :ref:`String<class_string>` saved in Pascal format from the file.
  190. .. _class_File_get_position:
  191. - :ref:`int<class_int>` **get_position** **(** **)** const
  192. Returns the file cursor's position.
  193. .. _class_File_get_real:
  194. - :ref:`float<class_float>` **get_real** **(** **)** const
  195. Returns the next bits from the file as a floating point number.
  196. .. _class_File_get_sha256:
  197. - :ref:`String<class_string>` **get_sha256** **(** :ref:`String<class_string>` path **)** const
  198. Returns a SHA-256 :ref:`String<class_string>` representing the file at the given path or an empty :ref:`String<class_string>` on failure.
  199. .. _class_File_get_var:
  200. - :ref:`Variant<class_variant>` **get_var** **(** **)** const
  201. Returns the next :ref:`Variant<class_variant>` value from the file.
  202. .. _class_File_is_open:
  203. - :ref:`bool<class_bool>` **is_open** **(** **)** const
  204. Returns ``true`` if the file is currently opened.
  205. .. _class_File_open:
  206. - :ref:`int<class_int>` **open** **(** :ref:`String<class_string>` path, :ref:`int<class_int>` flags **)**
  207. Opens the file for writing or reading, depending on the flags.
  208. .. _class_File_open_compressed:
  209. - :ref:`int<class_int>` **open_compressed** **(** :ref:`String<class_string>` path, :ref:`int<class_int>` mode_flags, :ref:`int<class_int>` compression_mode=0 **)**
  210. Opens a compressed file for reading or writing. Use COMPRESSION\_\* constants to set ``compression_mode``.
  211. .. _class_File_open_encrypted:
  212. - :ref:`int<class_int>` **open_encrypted** **(** :ref:`String<class_string>` path, :ref:`int<class_int>` mode_flags, :ref:`PoolByteArray<class_poolbytearray>` key **)**
  213. Opens an encrypted file in write or read mode. You need to pass a binary key to encrypt/decrypt it.
  214. .. _class_File_open_encrypted_with_pass:
  215. - :ref:`int<class_int>` **open_encrypted_with_pass** **(** :ref:`String<class_string>` path, :ref:`int<class_int>` mode_flags, :ref:`String<class_string>` pass **)**
  216. Opens an encrypted file in write or read mode. You need to pass a password to encrypt/decrypt it.
  217. .. _class_File_seek:
  218. - void **seek** **(** :ref:`int<class_int>` position **)**
  219. Change the file reading/writing cursor to the specified position (in bytes from the beginning of the file).
  220. .. _class_File_seek_end:
  221. - void **seek_end** **(** :ref:`int<class_int>` position=0 **)**
  222. 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.
  223. .. _class_File_set_endian_swap:
  224. - void **set_endian_swap** **(** :ref:`bool<class_bool>` enable **)**
  225. If ``true`` the file's endianness is swapped. Use this if you're dealing with files written in big endian machines.
  226. Note that this is about the file format, not CPU type. This is always reseted to ``false`` whenever you open the file.
  227. .. _class_File_store_16:
  228. - void **store_16** **(** :ref:`int<class_int>` value **)**
  229. Stores an integer as 16 bits in the file.
  230. .. _class_File_store_32:
  231. - void **store_32** **(** :ref:`int<class_int>` value **)**
  232. Stores an integer as 32 bits in the file.
  233. .. _class_File_store_64:
  234. - void **store_64** **(** :ref:`int<class_int>` value **)**
  235. Stores an integer as 64 bits in the file.
  236. .. _class_File_store_8:
  237. - void **store_8** **(** :ref:`int<class_int>` value **)**
  238. Stores an integer as 8 bits in the file.
  239. .. _class_File_store_buffer:
  240. - void **store_buffer** **(** :ref:`PoolByteArray<class_poolbytearray>` buffer **)**
  241. Stores the given array of bytes in the file.
  242. .. _class_File_store_double:
  243. - void **store_double** **(** :ref:`float<class_float>` value **)**
  244. Stores a floating point number as 64 bits in the file.
  245. .. _class_File_store_float:
  246. - void **store_float** **(** :ref:`float<class_float>` value **)**
  247. Stores a floating point number as 32 bits in the file.
  248. .. _class_File_store_line:
  249. - void **store_line** **(** :ref:`String<class_string>` line **)**
  250. Stores the given :ref:`String<class_string>` as a line in the file.
  251. .. _class_File_store_pascal_string:
  252. - void **store_pascal_string** **(** :ref:`String<class_string>` string **)**
  253. 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).
  254. .. _class_File_store_real:
  255. - void **store_real** **(** :ref:`float<class_float>` value **)**
  256. Stores a floating point number in the file.
  257. .. _class_File_store_string:
  258. - void **store_string** **(** :ref:`String<class_string>` string **)**
  259. Stores the given :ref:`String<class_string>` in the file.
  260. .. _class_File_store_var:
  261. - void **store_var** **(** :ref:`Variant<class_variant>` value **)**
  262. Stores any Variant value in the file.