> For the complete documentation index, see [llms.txt](https://chaoses-ib.gitbook.io/directory-opus/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chaoses-ib.gitbook.io/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/file.zh.md).

# File

**File** 对象允许您读取和写入磁盘上的文件（或 Zip 文件、FTP 站点等）中的二进制数据。虽然 Microsoft *Scripting.FileSystemObject* 对象允许您读取和写入文件，但它只支持文本，不支持二进制数据。您还可以使用 **File** 对象修改文件的属性和时间戳。

您可以使用 [**FSUtil**](/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/fsutil.zh.md)**.OpenFile** 和 [**Item**](/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/item.zh.md)**.Open** 方法获取 **File** 对象。您可以以三种模式之一打开文件：

* *读取模式* - 您可以在 **Read** 方法的帮助下从文件中读取数据。您不能写入它或修改它的属性。
* *写入模式* - 您可以在 **Write** 方法的帮助下向文件写入数据，并且您也可以修改文件的属性。您不能从中读取数据。
* *修改模式* - 您可以在 **Read** 方法的帮助下从文件中读取数据。您不能写入它或修改它的属性。

| 属性名称    | 返回类型                                                                                                     | 描述                                                                                                                                                                                                                                                                           |
| ------- | -------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| *<默认值>* | *字符串*                                                                                                    | 返回文件的完整路径名。                                                                                                                                                                                                                                                                  |
| error   | *int*                                                                                                    | <p>返回一个 Win32 错误代码，指示上次操作的成功或失败。如果前一次操作成功，通常为 <strong>0</strong>。</p><p>例如，如果您尝试使用 <strong>FSUtil.OpenFile</strong> 打开一个不存在的文件以进行读取，则将返回一个有效的 <strong>File</strong> 对象 - 但文件本身不会打开。您可以在继续使用 <strong>File</strong> 对象之前检查 <strong>error</strong> 是否返回 <strong>0</strong>。</p> |
| path    | *对象:*[**Path**](/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/path.zh.md)         | 返回文件的完整路径名，作为一个 [**Path**](/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/path.zh.md) 对象。                                                                                                                                                              |
| size    | *对象:*[**FileSize**](/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/filesize.zh.md) | 返回一个 [**FileSize**](/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/filesize.zh.md) 对象，表示此文件的大小（以字节为单位）。                                                                                                                                                |
| tell    | *对象:*[**FileSize**](/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/filesize.zh.md) | 返回一个 [**FileSize**](/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/filesize.zh.md) 对象，表示此文件内读写光标的当前位置（以字节为单位）。                                                                                                                                         |

| 方法名称       | **参数**                                                                                                                                                                   | 返回类型                                                                                                                                                     | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Close      | *无*                                                                                                                                                                      | *无*                                                                                                                                                      | <p>关闭底层文件句柄。在此调用之后，<strong>File</strong> 对象仍然有效，但它不能再读取或写入数据。</p><p>如果您想使用 <strong>SetAttr</strong> 方法修改您创建的文件的属性，您可能想要先调用 <strong>Close</strong>，否则文件系统将在您想要设置或不想要设置的情况下在文件上设置 <em>A</em>（压缩包）属性。</p><p>如果您想删除文件，您可能还想手动关闭文件，因为一些脚本语言（例如 <em>JScript</em>）具有延迟垃圾回收，否则可能会比您预期的更长时间保持文件句柄打开。</p>                                                                                                                                                                                                                                                                                                                                                                                 |
| Read       | <p><<a href="blob:target">blob:target</a>><br>\<int:size></p>                                                                                                            | <p><em>int</em> 或<br><em>对象:</em><a href="/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/blob.zh.md"><strong>Blob</strong></a></p> | <p>从文件读取数据。如果您提供一个 <em>target</em> <a href="/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/blob.zh.md"><strong>Blob</strong></a> 作为第一个参数，数据将存储在该 <strong>Blob</strong> 中。否则，将自动创建一个 <strong>Blob</strong>。<br>可选的 <em>size</em> 参数指定要读取的字节数 - 默认行为是读取文件的剩余内容。<br>每次调用最多可以读取 1 GB（1073741824 字节），无论是否指定或隐式指定大小。要读取更大的文件，您必须多次调用 <strong>Read</strong>。<br>如果您提供一个 <strong>Blob</strong>，则返回值表示从文件成功读取的字节数。如果没有提供 <strong>Blob</strong>，则返回值是自动创建的 <strong>Blob</strong> - 您可以使用它的 <strong>size</strong> 属性来发现读取的字节数。<br>如果 <strong>Read</strong> 返回零（或空 <strong>Blob</strong>），您可以使用 <strong>error</strong> 属性测试是否出现错误，或者文件是否 simply had no more data.</p> |
| Seek       | <p>\<int:delta><br>\<string:method></p>                                                                                                                                  | *对象:*[**FileSize**](/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/filesize.zh.md)                                                 | <p>移动此文件内的读写光标。<em>delta</em> 参数指定要移动的字节数 - 如何解释它取决于可选的 <em>method</em> 参数：</p><p><em>b</em> - 相对于文件开头移动<br><em>e</em> - 相对于文件结尾移动<br><em>c</em> - 相对于当前位置移动（这是默认方法）</p><p>返回值是一个 <a href="/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/filesize.zh.md"><strong>FileSize</strong></a> 对象，指示新的光标位置。</p>                                                                                                                                                                                                                                                                                                                                                  |
| SetAttr    | <p><em>对象:</em><a href="/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/fileattr.zh.md"><strong>FileAttr</strong></a><br>或 \<string:attributes></p> | *bool*                                                                                                                                                   | <p>修改此文件的属性。您可以传递一个表示要设置的属性的字符串，或者传递一个 <a href="/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/fileattr.zh.md"><strong>FileAttr</strong></a> 对象。使用字符串时，有效的属性为：</p><p><em>a</em> - 压缩包<br><em>c</em> - 压缩<br><em>e</em> - 加密<br><em>h</em> - 隐藏<br><em>n</em> - 正常<br><em>r</em> - 只读<br><em>s</em> - 系统<br>p - 固定<br>i - 非内容索引</p><p>请注意，<em>c</em> 和 <em>e</em> 属性不能同时设置。</p><p>当您传递字符串时，您还可以使用 <strong>+</strong> 和 <strong>-</strong> 来打开或关闭某些属性，而不会影响其它属性。例如，<strong>SetAttr("-r")</strong> 将关闭只读属性。</p><p>返回值为 <strong>True</strong>，如果操作成功。</p>                                                                                                     |
| SetTime    | <p>\<date:modify><br>\<date:create><br>\<date:access></p>                                                                                                                | *bool*                                                                                                                                                   | <p>修改一个或多个文件的时间戳。<em>create</em> 和 <em>access</em> 参数是可选的。如果您希望指定一个时间戳没有变化，请指定 <strong>0</strong>。</p><p>时间戳指定为本地时间 - 使用 <strong>SetTimeUTC</strong> 将它们指定为 UTC。</p><p>返回值为 <strong>True</strong>，如果操作成功。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| SetTimeUTC | <p>\<date:modify><br>\<date:create><br>\<date:access></p>                                                                                                                | *bool*                                                                                                                                                   | <p>修改一个或多个文件的时间戳。<em>create</em> 和 <em>access</em> 参数是可选的。如果您希望指定一个时间戳没有变化，请指定 <strong>0</strong>。</p><p>时间戳指定为 UTC 时间 - 使用 <strong>SetTime</strong> 将它们指定为本地时间。</p><p>返回值为 <strong>True</strong>，如果操作成功。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| Truncate   | *无*                                                                                                                                                                      | *bool*                                                                                                                                                   | <p>在写入光标的当前位置截断文件。您可以将此与 <strong>Seek</strong> 方法结合使用，以在磁盘上预分配文件的空间，从而提高性能（即，寻求文件的最终大小，在该点截断，然后返回到起点并写入数据）。</p><p>返回值为 <strong>True</strong>，如果操作成功。</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| Write      | <p><<a href="blob:source">blob:source</a>> 或 \<string:source><br>\<int:from><br>\<int:size></p>                                                                          | int                                                                                                                                                      | <p>将指定 <strong>Blob</strong>（或数组）或 <em>string</em> 中的数据写入文件。<br>默认情况下，将写入 <strong>Blob</strong> 的所有内容，但您可以使用可选的 <em>from</em> 参数指定源字节偏移量，使用 <em>size</em> 参数指定要写入的字节数。<br>每次调用最多可以写入 1 GB（1073741824 字节），无论是否指定或隐式指定大小。要写入更大的数据量，您必须多次调用 <strong>Write</strong>。<br>如果您提供 <em>string</em> 而不是 Blob，则该字符串将自动编码为 UTF-8。<br>返回值表示成功写入文件的字节数。<br>如果 <strong>Write</strong> 返回零，您可以使用 <strong>error</strong> 属性测试是否出现错误，或者是否 simply had no data to write（例如，指定的 <strong>Blob</strong> 为空）。</p>                                                                                                                                                                                       |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://chaoses-ib.gitbook.io/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh-3/readme.zh/file.zh.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
