Write a simple text / HTML file

How can I write a variable (or a table with only one column and one row) to a textfile?

I have a table which I want to output as an HTML file to a webserver.
The table → HTML part is solved (e.g., Table to HTML String – KNIME Community Hub and/or doing it myself). I only need a way to write the string to a file.

I know about Table to HTML – KNIME Community Hub, but the result is ugly and I can not configure it.

I tried to use the CSV writer, but it needs a column separation character. If this character appears anywhere in the file, it starts escaping it etc.

A simple “line writer” node would also be nice for other purposes, like writing a logfile, etc.

I could use a JAVA node to write the file, but I could not figure out how to use filesystem connectors from within the JAVA node.

Hi @masgo

I agree with you that using the CSV Writer for writing “plain text” files feels clumsy and a specific node would be an improvement. I’ve never really understood why there isn’t one.

The CSV Writer can generally do the job, but you’ll need to ensure that it is presented with only the one column, and probably the best answer I have for using it is to set the delimiter character to something that is unlikely to appear in your output.

For example the ¬ character is hardly used, or if you are using Windows, you can “dial in” numbers using the Alt key and the numeric key pad

e.g. press and hold the Alt key and dial 0172 using the numeric keypad keys [not the regular number keys] and this will also get you the ¬ character. Alt+0169 gets ©, Alt+0223 returns ß, Alt+0222 returns Þ , Alt+8223 gives▼ and so on.

Another option on Windows is to use the “emoji keyboard” popup. Press and hold the Windows Key, and then press the period/full-stop key “.”.
(Googling tells me this is Command + Control + Space on Mac, and possibly Ctrl ; on linux)

This will bring up the emoji window,
image

and you can choose a random emoji as the delimiter!

Beyond that, make sure double quotes are set to “never”

and Write column header is turned off

In terms of a java snippet, for writing to a text file, a simple example of writing to an absolute file path can be found here:

but you also mention File System Connectors - what type of file system are you trying to connect to?

3 Likes

To my surpise, I found a solution. It’s called “Binary Objects”, which is not intuitive at all. A simple example would look like this:

grafik

The Table Creator creates a table with two columns: “test” containing the string I want to write to a file and “filename” containing the desired file.

The Strings to Binary Objects node even allows to set the encoding:
grafik

The Binary Objects to File node does support File System connectors.
The way to specify the filename is a little strange (one can not use variables), but works fine:

In my real-world scenario I am using it like so:
grafik

Two tables are converted to HTML, then the Filename is added as Column, then concatenate both, to Binary Objects and write the files via SSH. Works great.

3 Likes

Hi @masgo, I’m glad you got that working. I have recently used Binary Objects to Files myself, but only for binary objects! I hadn’t thought about using it for writing text.

The way it works for specifying the file (using the name of the file found in the column) is similar to the mechanism for nodes such as JSON Writer, as it is designed for being able to rapidly write each row to a separate file. In your case of course you have just the one row.

Having just looked at it again, along with your solution, I see why I’d not considered its use for writing plain text - it requires data to be in a binary-compatible format, which plain text Strings wouldn’t be and so of course Strings to Binary Objects resolves that issue.

Anyway, glad it works for you and thanks for sharing the solution. That might be useful in something I’m working on right now! :slight_smile: I’d give your solution two “likes” if I could :wink:

p.s. and I think you should mark your own answer as “Solution”

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.