Water 5-System Interfaces-FilesystemInsert content into a file
Contract| Return type | wob | | Parameter key | Default value | Type | | at_key | "end" | string | | Parameter kind | Default value | Type | | Other unkeyed arguments | req | | Water Contract<method resource.filesystem.file.insert
at_key="end"
_other_unkeyed=req=wob=ekind.code="_body"/> | |
See also: append, set, is_writable, file
Works similar to insert on strings, inserting the _other_unkeyed
args into the content of the _subject of type file .
The new content of the file is returned.
If a location is not specified, it appends content
at the end of the file.
If an arg is not already a string, it is converted before it is written
to the file.
If the file is not writable, insert will throw an error.
Example: Insert at end of file (default)
<class foo>
<method htm_inst>
"foo htm instance"
</method>
</class>
<file "logical://user/test_file.txt" />.<insert <join char.carriage_return char.newline <foo /> /> />
Example: Insert at beginning of file
<class foo>
<method htm_inst>
"foo htm instance"
</method>
</class>
<file "logical://user/test_file.txt" />.<insert at_key=0 <join <foo /> char.carriage_return char.newline /> />
Example: Insert at a specified location
<set the_file=<file "logical://user/protected folder/test_file.txt" /> />
<set the_content=the_file.content />
<set the_location=the_content.<key_of char.newline /> />
<set the_location=
<if> the_location.<is false />
0
else
the_location.<plus 1 />
</if>
/>
the_file.<insert at_key=the_location <join "second line" char.carriage_return char.newline /> />
The example above finds the second line of the text file and inserts the
text "second line" in front of it.
Each of the examples above would return the new contents of the specified file
if the file is writable.
© Copyright 2007 Clear Methods, Inc.