readFile is lazy, and writeFile to the same file can fail unless one makes sure that the whole file has already been read. In the function above this is solved by asking for the length of the new string before writing the file (the seq function makes sure that the length is computed before the write operation happens).

the file may not exist, the catch clause is used above to handle that exceptional case.