Share your thoughts in the 2021 Clojure Community Survey!

Welcome! Please see the About page for a little more info on how this works.

0 votes
in ClojureCLR by

For example:

(spit "c:/tmp/spit.txt" "012345")
(spit "c:/tmp/spit.txt" "BB")

Results in a file containing: BB345.

Reported via email by cees van Kemenade.

6 Answers

0 votes
by

Comment made by: dmiller

spit with a string argument defaults to opening a FileStream (wrapped in a StreamWriter) in mode FileMode.CreateOrNew. This will work whether or not the file exists. To default to Truncate will fail if the file does not exist. For this reason, I don't want to make it the default. I'm also not sure how to play with tests of file existence versus given file-mode to determine what action to take.

If you know the file exists and want to truncate, call spit with that file mode.

(spit "filename" "test" :file-mode System.IO.FileMode/Truncate)

0 votes
by

Comment made by: dmiller

Correct action is to call spit specifying the file mode:

(spit "c:/tmp/spit.txt" "BB" :file-mode System.IO.FileMode/Truncate)

0 votes
by

Comment made by: dmiller

At the request of the OP, I have decided to reconsider this issue.

0 votes
by

Comment made by: dmiller

Cees suggests the attached code.

0 votes
by

Comment made by: dmiller

Adding to this: change slurp to read the file in non-locking mode.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJCLR-37 (reported by dmiller)
...