File Writer allows information to be written in a file.

Take a look at the configuration parameters of the component:

  • File Name: determines the name of the file to be generated by the component with the input information. This parameter accepts Double Braces.

  • Data: determines the data that must be written in the file generated by the component. The field accepts an array of strings a simple string - if it’s an array, each one of its items will be recorded in a new line; but if the content is an object (and not a string), then the “Coalesce” property must be enabled to avoid an error occurrence. This parameter accepts Double Braces.

  • Policy For When File Already Exists: parameter in which the behavior to be followed is configured if a file with the same name already exists in the current execution. There’re these options and definitions:

- APPEND: the data is added in the existing file;

- OVERRIDE: the existing file is overridden;

- FAIL: the flow is interrupted by an error.

  • End of Line Policy: determines the use of end-of-line characters policy. There’re these options and definitions:

- WINDOW: 2 characters are used for the end of the line (CR + LF);

- UNIX: only 1 character is used (LF);

- NONE: no character applies.

  • Charset: determines the characters code to be used for the file creation. The standard is UTF-8.

  • Binary File: if the data input for the component (determined in the Data parameter) is a base64-type string and this option is enabled, then the text will be converted and recorded in the file.

  • Coalesce: if the option is enabled and an input message value corresponds to some object/array, the informed data will be accepted by the component and the file will be successfully recorded; otherwise, when receiving a value as object/array, an error will be presented as the result and “false” will be shown for the success property.

  • Fail On Error: if the option is enabled, the pipeline execution will be interrupted; otherwise, the pipeline execution continues, but the result will show a false value for the success property.

IMPORTANT: mind some of the parameters above support Double Braces. To understand how this language works, read our article by clicking here.

Files manipulation in the pipeline

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

  • 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

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 component.

The File Writer component will be configured the following way:

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

{
"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 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 component.

The File Writer component will be configured the following way:

Input

{
"data": "VG8gS2lsbCBhIE1vY2tpbmdiaXJkCjE5ODQKSGFycnkgUG90dGVyIGFuZCB0aGUgUGhpbG9zb3BoZXLigJlzIFN0b25lClRoZSBMb3JkIG9mIHRoZSBSaW5ncwpUaGUgR3JlYXQgR2F0c2J5ClByaWRlIGFuZCBQcmVqdWRpY2UKVGhlIERpYXJ5IE9mIEEgWW91bmcgR2lybApUaGUgQm9vayBUaGllZgpUaGUgSG9iYml0CkxpdHRsZSBXb21lbgpGYWhyZW5oZWl0IDQ1MQpKYW5lIEV5cmUKQW5pbWFsIEZhcm0KR29uZSB3aXRoIHRoZSBXaW5kClRoZSBDYXRjaGVyIGluIHRoZSBSeWUKQ2hhcmxvdHRl4oCZcyBXZWIKVGhlIExpb24sIHRoZSBXaXRjaApUaGUgR3JhcGVzIG9mIFdyYXRoCkxvcmQgb2YgdGhlIEZsaWVzClRoZSBLaXRlIFJ1bm5lcgpPZiBNaWNlIGFuZCBNZW4KQSBUYWxlIG9mIFR3byBDaXRpZXMKUm9tZW8gYW5kIEp1bGlldApUaGUgSGl0Y2hoaWtlcnMgR3VpZGUgdG8gdGhlIEdhbGF4eQpXdXRoZXJpbmcgSGVpZ2h0cwpUaGUgQ29sb3IgUHVycGxlCkFsaWNlIGluIFdvbmRlcmxhbmQKRnJhbmtlbnN0ZWluClRoZSBBZHZlbnR1cmVzIG9mIEh1Y2tsZWJlcnJ5IEZpbm4KU2xhdWdodGVyaG91c2UtRml2ZQ=="
}

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 component.

The File Writer component will be configured the following way:

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, 2 File Writer components will be configured, one after the other with the options.

The File Writer component will be configured the following way:

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

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

Did this answer your question?