class_file.rst 29 KB

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