> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kasoftware.com/llms.txt
> Use this file to discover all available pages before exploring further.

# fileRead

> 读取文件内容，支持编码转换和文本替换。

读取输入文件的内容并将数据作为输出项的属性推出。

## 必需的参数

* **file**: 要读取的文件名称。

## 可选的参数

* **encoding**：用于将文件的原始字节转换为字符串的策略。如果提供了标准字符集（UTF-8、ASCII 等），操作将使用提供的字符集对字节进行\_解码\_。如果提供了二进制值（BASE64、HEX 等），操作将使用提供的值对原始字节进行\_编码\_。encoding 参数中大多数操作系统和 JVM 通常支持的值包括 `UTF-8`、`ASCII`、`BASE64`、`HEX`、`windows-1252` 和 `ISO-8859-2`。默认为 `UTF-8`。

## 输出属性

* **file:data**：输入文件中的数据。

## 示例

### 更改输入文件的编码

```xml theme={null}
<!-- Creating the input item and setting the file and encoding attributes -->
<arc:set attr="input.file" value="[FilePath]" />
<arc:set attr="input.encoding" value="BASE64" />
<!-- Calling fileRead and passing in the input item -->
<arc:call op="fileRead" in="input" out="result" >
  <!-- The file:data here is now BASE64 encoded based on the value set in input.encoding -->
  <arc:set attr="fileOut.data" value="[result.file:data]" />
</arc:call>

<!-- Checking to make sure the output file has data, else throw an error -->
<arc:check attr="fileOut.data" >
  <arc:set attr="fileOut.filename" value="[FileName]" />
  <arc:push item="fileOut" />
  <arc:else>
    <arc:throw code="NoData" desc="No file data." />
  </arc:else>
</arc:check>
```

### 将逗号替换为竖线字符 ( | )

```xml theme={null}
<!-- Creating the input item and setting the file attribute -->
<arc:set attr="input.file" value="[FilePath]" />
<!-- Calling fileRead and passing in the input item -->
<arc:call op="fileRead" in="input" out="result">]
  <!-- Replacing all commas in the file with pipes and setting the new data on the output item -->
  <arc:set attr="output.data" value="[result.file:data | replace(',','|')]" />
</arc:call>

<!-- Checking to make sure the output file has data, else throw an error -->
<arc:check attr="output.data" >
  <arc:set attr="output.filename" value="[FileName]" />
  <arc:push item="output" />
  <arc:else>
    <arc:throw code="NoData" desc="No file data." />
  </arc:else>
</arc:check>
```
