---
title: "文件 File / Fs"
description: "文件读写、目录与查找"
---

---
title: 文件 File / Fs
description: 文件读写、目录与查找
---

`gta3` · 扩展 `CLEO` · 需 CLEO

写法见 [Lua](/docs/cleo/syntax) / [Redux](/docs/cleo/syntax-redux)。

## 设工作目录

<Opcode id="0A99" name="SET_CURRENT_DIRECTORY" member="Fs.SetCurrentDirectory">
设工作目录。

```text
0A99 SET_CURRENT_DIRECTORY
in: path
```

```lua
setCurrentDirectory(0)
```

```js
Fs.setCurrentDirectory(path)
```
</Opcode>

## 打开文件

<Opcode id="0A9A" name="OPEN_FILE" member="File.Open">
打开文件。

```text
0A9A OPEN_FILE
in: filePathName, mode
out: handle
```

```lua
local f = openFile("cleo/data.txt", "rb")
```

```js
const handle = File.open(filePathName, mode)
```
</Opcode>

## 关闭文件

<Opcode id="0A9B" name="CLOSE_FILE" member="File.Close">
关闭文件。

```text
0A9B CLOSE_FILE
in: self
```

```lua
closeFile(f)
```

```js
File.close(self)
```
</Opcode>

## 文件大小

<Opcode id="0A9C" name="GET_FILE_SIZE" member="File.GetSize">
文件大小。

```text
0A9C GET_FILE_SIZE
in: self
out: size
```

```lua
local size = getFileSize(f)
```

```js
const size = File.getSize(self)
```
</Opcode>

## 读文件

<Opcode id="0A9D" name="READ_FROM_FILE" member="File.Read">
读字节。

```text
0A9D READ_FROM_FILE
in: self, size
out: destination
```

```lua
local data = readFromFile(f, 4)
```

```js
const destination = File.read(self, size)
```
</Opcode>

## 写文件

<Opcode id="0A9E" name="WRITE_TO_FILE" member="File.Write">
写字节。

```text
0A9E WRITE_TO_FILE
in: self, size, source
```

```lua
writeToFile(f, 4, data)
```

```js
File.write(self, size, source)
```
</Opcode>

## 文件是否存在

<Opcode id="0AAB" name="DOES_FILE_EXIST" member="Fs.DoesFileExist">
文件是否存在。

```text
0AAB DOES_FILE_EXIST
in: path
```

```lua
if doesFileExist("cleo/a.ini") then
end
```

```js
if (Fs.doesFileExist(path)) {}
```
</Opcode>

## 文件指针移动

<Opcode id="0AD5" name="FILE_SEEK" member="File.Seek">
移动文件指针。

```text
0AD5 FILE_SEEK
in: self, offset, origin
```

```lua
fileSeek(f, 0, 0)
```

```js
File.seek(self, offset, origin)
```
</Opcode>

## 是否到文件尾

<Opcode id="0AD6" name="IS_END_OF_FILE_REACHED" member="File.IsEndReached">
是否 EOF。

```text
0AD6 IS_END_OF_FILE_REACHED
in: self
```

```lua
if isEndOfFileReached(f) then
end
```

```js
if (File.isEndReached(self)) {}
```
</Opcode>

## 读字符串

<Opcode id="0AD7" name="READ_STRING_FROM_FILE" member="File.ReadString">
读一行字符串。

```text
0AD7 READ_STRING_FROM_FILE
in: self, storeTo, maxLength
```

```lua
readStringFromFile(f, buf, 128)
```

```js
File.readString(self, storeTo, maxLength)
```
</Opcode>

## 写字符串

<Opcode id="0AD8" name="WRITE_STRING_TO_FILE" member="File.WriteString">
写字符串。

```text
0AD8 WRITE_STRING_TO_FILE
in: self, source
```

```lua
writeStringToFile(f, "line\n")
```

```js
File.writeString(self, source)
```
</Opcode>

## 格式化写文件

<Opcode id="0AD9" name="WRITE_FORMATTED_STRING_TO_FILE" member="File.WriteFormattedString">
格式化写文件。

```text
0AD9 WRITE_FORMATTED_STRING_TO_FILE
in: self, format, args...
```

```lua
writeFormattedStringToFile(f, "%d", 1)
```

```js
File.writeFormattedString(self, format, ...args)
```
</Opcode>

## 扫描文件

<Opcode id="0ADA" name="SCAN_FILE" member="File.Scan">
fscanf。

```text
0ADA SCAN_FILE
in: self, format
out: nValues, values...
```

```lua
local n, a = scanFile(f, "%d")
```

```js
const [nValues, ...values] = File.scan(self, format)
```
</Opcode>

## 目录是否存在

<Opcode id="0AE4" name="DOES_DIRECTORY_EXIST" member="Fs.DoesDirectoryExist">
目录是否存在。

```text
0AE4 DOES_DIRECTORY_EXIST
in: path
```

```lua
if doesDirectoryExist("cleo") then
end
```

```js
if (Fs.doesDirectoryExist(path)) {}
```
</Opcode>

## 创建目录

<Opcode id="0AE5" name="CREATE_DIRECTORY" member="Fs.CreateDirectory">
创建目录。条件表示是否成功。

```text
0AE5 CREATE_DIRECTORY
in: path
```

```lua
createDirectory("cleo/out")
```

```js
Fs.createDirectory(path)
```
</Opcode>

## 查找首个文件

<Opcode id="0AE6" name="FIND_FIRST_FILE" member="FindFile.First">
通配找第一个匹配项，返回查找句柄和名字。

```text
0AE6 FIND_FIRST_FILE
in: searchMask
out: handle, fileName
```

```lua
local h, name = findFirstFile("cleo/*.cs")
```

```js
const [handle, fileName] = FindFile.first(searchMask)
```
</Opcode>

## 查找下一个文件

<Opcode id="0AE7" name="FIND_NEXT_FILE" member="FindFile.Next">
继续 `FIND_FIRST_FILE`。

```text
0AE7 FIND_NEXT_FILE
in: self
out: fileName
```

```lua
local name = findNextFile(h)
```

```js
const fileName = FindFile.next(self)
```
</Opcode>

## 关闭查找

<Opcode id="0AE8" name="FIND_CLOSE" member="FindFile.Close">
关闭查找句柄。

```text
0AE8 FIND_CLOSE
in: self
```

```lua
findClose(h)
```

```js
FindFile.close(self)
```
</Opcode>