class_file.rst 30 KB

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