Skip to content

Commit cc24866

Browse files
committed
introduce appendLines method in IO.
Adds appendLines to the IO api, which lets users easily append a sequence of strings to a file that already exists.
1 parent 35a4d2f commit cc24866

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/main/scala/introprog/IO.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ object IO:
4949
def saveLines(lines: Seq[String], fileName: String, enc: String = "UTF-8"): Unit =
5050
saveString(lines.mkString("\n"), fileName, enc)
5151

52+
/**
53+
* Appends `lines` to the text file `fileName` using encoding `enc`.
54+
*
55+
* @param lines the lines to append to the file.
56+
* @param fileName the path of the file.
57+
* @param enc the encoding of the file.
58+
* */
59+
def appendLines(lines: Seq[String], fileName: String, enc: String = "UTF-8"): Unit =
60+
val newLines = loadLines(fileName, enc).appendedAll(lines)
61+
saveLines(newLines, fileName, enc)
62+
5263
/**
5364
* Load a serialized object from a binary file called `fileName`.
5465
*

0 commit comments

Comments
 (0)