# read-file

Learn how to read file content into a variable.

This action reads the content of a file and stores it in a variable.

The content is available as a string for use in subsequent actions, such as printing, patching, or conditional logic.

## Example

The following template reads a file and prints its content:

**template.json5**

```json5
{
  "$schema": "https://schema.croct.com/json/v1/template.json",
  "title": "File reader",
  "description": "Reads and displays a file.",
  "actions": [
    {
      "name": "read-file",
      "path": "README.md",
      "result": "content"
    },
    {
      "name": "print",
      "semantics": "info",
      "message": "${this.content}"
    }
  ]
}
```

To apply this template, run:

```sh
croct use template.json5
```

## Properties

These are the supported properties:

- `name`: `string`

  The action identifier. Always `read-file` for this action.

- `path`: `string`

  The path to the file to read.

- `result`: `string`

  The variable name to store the file content as a string.

- `optional`: `boolean` (optional) (default: false)

  Whether to store `null` instead of failing when the file does not exist or is a directory.

## Explore

- [Write file](/reference/cli/templates/actions/write-file): Learn how to write content to a file.
- [Replace file content](/reference/cli/templates/actions/replace-file-content): Learn how to replace content in the file.
- [Define](/reference/cli/templates/actions/define): Learn how to store the read content in a variable.
- [Locate path](/reference/cli/templates/actions/locate-path): Learn how to find the file path to read.
