> ## 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>
```
