Skip to content

Routes: File Writer

TekMonks edited this page Mar 14, 2019 · 2 revisions

The File Writer Route

The File Writer route node is used to write data to files. The file write node writes the entire message to the file at once. However, it is capable of appending, thus, writing multiple messages to the same file in append mode, if configured to do so.

This node does not parse the parse the data itself and no assumptions are made about the file data format. The property message.content contains the data to write out.

The code block below documents the format for the File Writer route node.

"route0": {
	"type": "filewriter",
	"dependencies":"listener",
	"path":"c:/test/Test.json",
	"prettyJSON": 4,
	"append": false,
	"writeCloseTimeout": 5000
}

The path property is used to point to the file to write to.

The append property indicates whether to append or overwrite/create a new file.

The prettyJSON property writes the message.content to a JSON file in pretty mode. The value is the number of spaces to use for tabbing. This property is only used if the message.content is a parsable JSON object.

The writeCloseTimeout is in milliseconds. It indicates the number of seconds the file writer waits, for any output, before it closes the file handle. This is used for enhancing the performance and reduces the open file handles the code needs to maintain. For example, if millions of messages are being written to the file, the file writer with a 5 second timeout will not keep opening new file handles for each message, as long as that message arrives within a 5 second window.

The following code block shows the file writer being used together to transform an XML file to a JSON file.

{
	"flow":{
		"name":"XML to JSON",
		"disabled":false
	},
	"listener": {
		"type":"file", 
		"isMessageGenerator": true,
		"path":"c:/test/in/Test.xml",
		"donePath":"c:/test/processing"
	},
	"route0":{
		"type": "filereader",
		"dependencies":["listener"],
		"donePath":"c:/test/done",
		"encoding":"utf8"
	},
	"route1": {
		"type":"xmlparser",
		"dependencies":["route0"]
	},
	"output": {
		"type": "filewriter",
		"dependencies":"route1",
		"path":"c:/test/Test.json",
		"prettyJSON": 4,
		"append": false,
		"writeCloseTimeout": 5000
	}
}