Skip to content

Commit 482dcce

Browse files
committed
fix saveLines appendLines so that empty and trailing newline is correct
1 parent 315e9a2 commit 482dcce

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/main/scala/introprog/IO.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ object IO:
5151
* @param enc the encoding of the file.
5252
* */
5353
def saveLines(lines: Seq[String], fileName: String, enc: String = "UTF-8"): Unit =
54-
saveString(lines.mkString("\n") + "\n", fileName, enc)
54+
if lines.nonEmpty then saveString(lines.mkString("", "\n", "\n"), fileName, enc)
5555

5656
/**
5757
* Appends `string` to the text file `fileName` using encoding `enc`.
@@ -78,7 +78,7 @@ object IO:
7878
* @param enc the encoding of the file.
7979
* */
8080
def appendLines(lines: Seq[String], fileName: String, enc: String = "UTF-8"): Unit =
81-
appendString(lines.mkString("\n"), fileName, enc)
81+
if lines.nonEmpty then appendString(lines.mkString("","\n","\n"), fileName, enc)
8282

8383
/**
8484
* Load a serialized object from a binary file called `fileName`.

0 commit comments

Comments
 (0)