# File Writer

**File Writer** allows information to be written in a file.

## Parameters

Take a look at the configuration parameters of the component. Parameters supported by [Double Braces expressions](https://docs.digibee.com/documentation/connectors-and-triggers/double-braces/overview) are marked with `(DB)`.

<table data-full-width="true"><thead><tr><th>Parameter</th><th width="322">Description</th><th>Default value</th><th>Data type</th></tr></thead><tbody><tr><td><strong>File Name</strong> <code>(DB)</code></td><td>File name or full file path (i.e. tmp/processed/file.txt) of the file to be generated by the component with the input information.</td><td>data.csv</td><td>String</td></tr><tr><td><strong>Data</strong> <code>(DB)</code></td><td>Determines the data that must be written in the file generated by the component. The field accepts an array of strings or a simple string. If it’s an array, each item will be recorded in a new line. If the content is an object (and not a string), then the <strong>Coalesce</strong> property must be enabled to avoid an error occurrence.</td><td>{{ message.data }}</td><td>String</td></tr><tr><td><strong>Policy For When File Already Exists</strong></td><td>Parameter in which the behavior to be followed is configured if a file with the same name already exists in the current execution. Options: Append (data added in the existing file), Override (existing file is overridden), Fail (flow is interrupted by an error).</td><td>Append</td><td>String</td></tr><tr><td><strong>End of Line Policy</strong></td><td>Determines the use of end-of-line characters policy. Options: Windows (2 characters for end of line - CR + LF), Unix (only 1 character for end of line - LF), None (no character applies).</td><td>Windows</td><td>String</td></tr><tr><td><strong>Charset</strong></td><td>Determines the characters code to be used for file creation. </td><td>UTF-8</td><td>String</td></tr><tr><td><strong>Binary File</strong></td><td>If the data input for the component (determined in the <strong>Data</strong> parameter) is a base64-type string and this option is enabled, then the text will be converted and recorded in the file.</td><td>False</td><td>Boolean</td></tr><tr><td><strong>Coalesce</strong></td><td>If the option is enabled and the value of an input message corresponds to an object/array, the informed data will be accepted by the component, and the file will be successfully recorded. Otherwise, when receiving a value as an object/array, an error will be presented as a result, and "false" will be shown for the "success" property.</td><td>False</td><td>Boolean</td></tr><tr><td><strong>Fail On Error</strong></td><td>If the option is enabled, the execution of the pipeline with an error will be interrupted; otherwise, the pipeline execution proceeds, but the result will show a false value for the "success" property.</td><td>False</td><td>Boolean</td></tr></tbody></table>

## Files manipulation in the pipeline <a href="#h_0503b11733" id="h_0503b11733"></a>

The pipeline has a local and temporary area for the files manipulation, which is separated and validated only during the flow execution.

That way, you must understand the access to the files as if it was made in a virtual file system. The files names can have any valid character and file extensions, which can also have a relative directory.

For example: data.csv or processing/data.csv.

Any attempt to access other absolute directories will be blocked during the pipeline execution.

## Messages flow <a href="#h_7ef9b2fdaa" id="h_7ef9b2fdaa"></a>

### **Input**

The component accepts any input message, being able to use it through Double Braces.

### **Output**

The component returns a JSON containing the name of the created file and the `success` property containing the value `true`.

* **Without error**

```
{
"fileName": "data.csv",
"success": true
}
```

* **With error**

```
{
"success": false,
"message": "File data.csv already exists.",
"exception":
"com.digibee.pipelineengine.exception.PipelineEngineRuntimeException"
}
```

## File Writer in Action <a href="#h_984cc503b2" id="h_984cc503b2"></a>

You’ll see below how the component behaves in determined situations and what its respective configuration is.

### **Create txt file with a string sent by Double Braces**

For this example, a static data input will be used and, by the end, the file will be read with the [**File Reader**](https://docs.digibee.com/documentation/connectors-and-triggers/connectors/files/file-reader) componen&#x74;**.**

The **File Writer** component will be configured the following way:

* **File Name:** booklist.txt
* **Data:** {{ message.data }}
* **Policy For When File Already Exists:** Append
* **End of Line Policy:** Windows
* **Charset:** UTF-8
* **Binary File:** deactivated
* **Coalesce:** deactivated
* **Fail on Error:** deactivated

#### **Input**

```
{
"data": "To Kill a Mockingbird\n1984\nHarry Potter and the Philosopher’s Stone\nThe Lord of the Rings\nThe Great Gatsby\nPride and Prejudice\nThe Diary Of A Young Girl\nThe Book Thief\nThe Hobbit\nLittle Women\nFahrenheit 451\nJane Eyre\nAnimal Farm\nGone with the Wind\nThe Catcher in the Rye\nCharlotte’s Web\nThe Lion, the Witch\nThe Grapes of Wrath\nLord of the Flies\nThe Kite Runner\nOf Mice and Men\nA Tale of Two Cities\nRomeo and Juliet\nThe Hitchhikers Guide to the Galaxy\nWuthering Heights\nThe Color Purple\nAlice in Wonderland\nFrankenstein\nThe Adventures of Huckleberry Finn\nSlaughterhouse-Five"
}
```

#### **Output**

```
{
"fileName": "booklist.txt",
"success": true
}
```

* **fileName:** name of the written file.
* **success:** if “true”, the operation has been successfully executed; if “false”, the **Fail On Error** property was enabled.

#### **Created file reading**

{% code overflow="wrap" %}

```
{"data": ["To Kill a Mockingbird","1984","Harry Potter and the Philosopher’s Stone","The Lord of the Rings","The Great Gatsby","Pride and Prejudice","The Diary Of A Young Girl","The Book Thief","The Hobbit","Little Women","Fahrenheit 451","Jane Eyre","Animal Farm","Gone with the Wind","The Catcher in the Rye","Charlotte’s Web","The Lion, the Witch","The Grapes of Wrath","Lord of the Flies","The Kite Runner","Of Mice and Men","A Tale of Two Cities","Romeo and Juliet","The Hitchhikers Guide to the Galaxy","Wuthering Heights","The Color Purple","Alice in Wonderland","Frankenstein","The Adventures of Huckleberry Finn","Slaughterhouse-Five"],"fileName": "booklist.txt","lineCount": 30}
```

{% endcode %}

### **Create txt file with a data in base64 that will be converted when recording the file**

For this example, a static data input will be used and, by the end, the file will be read with the [**File Reader**](https://docs.digibee.com/documentation/connectors-and-triggers/connectors/files/file-reader) componen&#x74;**.**

The **File Writer** component will be configured the following way:

* **File Name:** booklist.txt
* **Data:** {{ message.data }}
* **Policy For When File Already Exists:** Append
* **End of Line Policy:** Windows
* **Charset:** UTF-8
* **Binary File:** activated
* **Coalesce:** deactivated
* **Fail on Error:** deactivated

#### **Input**

{% code fullWidth="false" %}

```
{
"data": "VG8gS2lsbCBhIE1vY2tpbmdiaXJkCjE5ODQKSGFycnkgUG90dGVyIGFuZCB0aGUgUGhpbG9zb3BoZXLigJlzIFN0b25lClRoZSBMb3JkIG9mIHRoZSBSaW5ncwpUaGUgR3JlYXQgR2F0c2J5ClByaWRlIGFuZCBQcmVqdWRpY2UKVGhlIERpYXJ5IE9mIEEgWW91bmcgR2lybApUaGUgQm9vayBUaGllZgpUaGUgSG9iYml0CkxpdHRsZSBXb21lbgpGYWhyZW5oZWl0IDQ1MQpKYW5lIEV5cmUKQW5pbWFsIEZhcm0KR29uZSB3aXRoIHRoZSBXaW5kClRoZSBDYXRjaGVyIGluIHRoZSBSeWUKQ2hhcmxvdHRl4oCZcyBXZWIKVGhlIExpb24sIHRoZSBXaXRjaApUaGUgR3JhcGVzIG9mIFdyYXRoCkxvcmQgb2YgdGhlIEZsaWVzClRoZSBLaXRlIFJ1bm5lcgpPZiBNaWNlIGFuZCBNZW4KQSBUYWxlIG9mIFR3byBDaXRpZXMKUm9tZW8gYW5kIEp1bGlldApUaGUgSGl0Y2hoaWtlcnMgR3VpZGUgdG8gdGhlIEdhbGF4eQpXdXRoZXJpbmcgSGVpZ2h0cwpUaGUgQ29sb3IgUHVycGxlCkFsaWNlIGluIFdvbmRlcmxhbmQKRnJhbmtlbnN0ZWluClRoZSBBZHZlbnR1cmVzIG9mIEh1Y2tsZWJlcnJ5IEZpbm4KU2xhdWdodGVyaG91c2UtRml2ZQ=="
}
```

{% endcode %}

#### **Output**

```
{
"fileName": "booklist.txt",
"success": true
}
```

#### **Created file reading**

```
{
"data": [
"To Kill a Mockingbird",
"1984",
"Harry Potter and the Philosopher’s Stone",
"The Lord of the Rings",
"The Great Gatsby",
"Pride and Prejudice",
"The Diary Of A Young Girl",
"The Book Thief",
"The Hobbit",
"Little Women",
"Fahrenheit 451",
"Jane Eyre",
"Animal Farm",
"Gone with the Wind",
"The Catcher in the Rye",
"Charlotte’s Web",
"The Lion, the Witch",
"The Grapes of Wrath",
"Lord of the Flies",
"The Kite Runner",
"Of Mice and Men",
"A Tale of Two Cities",
"Romeo and Juliet",
"The Hitchhikers Guide to the Galaxy",
"Wuthering Heights",
"The Color Purple",
"Alice in Wonderland",
"Frankenstein",
"The Adventures of Huckleberry Finn",
"Slaughterhouse-Five"
],
"fileName": "booklist.txt",
"lineCount": 30
}
```

### **Create txt file with an input data made through a multilevel JSON**

For this example, a static data input will be used and, by the end, the file will be read with the [**File Reader** ](https://docs.digibee.com/documentation/connectors-and-triggers/connectors/files/file-reader)componen&#x74;**.**

The **File Writer** component will be configured the following way:

* **File Name:** product.txt
* **Data:** {{ message.data }}
* **Policy For When File Already Exists:** Append
* **End of Line Policy:** Windows
* **Charset:** UTF-8
* **Binary File:** deactivated
* **Coalesce:** activated
* **Fail on Error:** deactivated

#### **Input**

```
{
"data": {
"products": [
{
"name": "Samsung 4k Q60T 55",
"price": 3278.99
},
{
"name": "Samsung galaxy S20 128GB",
"price": 3698.99
}
]
}
}
```

#### **Output**

```
{
"fileName": "product.txt",
"success": true
}
```

#### **Created file reading**

```
{
"data": [
"{\"products\":[{\"name\":\"Samsung 4k Q60T 55\",\"price\":3278.99},{\"name\":\"Samsung galaxy S20 128GB\",\"price\":3698.99}]}"
],
"fileName": "product.txt",
"lineCount": 1
}
```

That way, the multilevel JSON informed to the component will be inserted as a single line in the TXT file.

### **Existing file in the execution will the failure policy**

For this example, two **File Writer** components will be configured, one after the other with the options.

The **File Writer** component will be configured the following way:

* **File Name:** booklist.txt
* **Data:** {{ message.data }}
* **Policy For When File Already Exists:** Fail
* **End of Line Policy:** Windows
* **Charset:** UTF-8
* **Binary File:** deactivated
* **Coalesce:** deactivated
* **Fail on Error:** deactivated

By the end, the canvas will have the following format:

<figure><img src="https://content.gitbook.com/content/EKM2LD3uNAckQgy1OUyZ/blobs/Y5NPEExzngXhna1JmwNE/file%20writer%20pipeline%20example%2014%20nov%2023.png" alt=""><figcaption></figcaption></figure>

#### **Input**

```
{
"data": "To Kill a Mockingbird\n1984\nHarry Potter and the Philosopher’s Stone\nThe Lord of the Rings\nThe Great Gatsby\nPride and Prejudice\nThe Diary Of A Young Girl\nThe Book Thief\nThe Hobbit\nLittle Women\nFahrenheit 451\nJane Eyre\nAnimal Farm\nGone with the Wind\nThe Catcher in the Rye\nCharlotte’s Web\nThe Lion, the Witch\nThe Grapes of Wrath\nLord of the Flies\nThe Kite Runner\nOf Mice and Men\nA Tale of Two Cities\nRomeo and Juliet\nThe Hitchhikers Guide to the Galaxy\nWuthering Heights\nThe Color Purple\nAlice in Wonderland\nFrankenstein\nThe Adventures of Huckleberry Finn\nSlaughterhouse-Five"
}
```

#### **Output**

```
{
"success": false,
"message": "File booklist.txt already exists.",
"exception": "com.digibee.pipelineengine.exception.PipelineEngineRuntimeException"
}
```

* **success:** “false” when the operation fails.
* **message:** message about the error.
* **exception:** information about the error.
