Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/main/scala/introprog/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ object IO:
* @param text the text to be written to the file.
* @param fileName the path of the file.
* @param enc the encoding of the file.
* @param isOverwrite should the file be overwritten if it already exists
* */
def saveString(text: String, fileName: String, enc: String = "UTF-8"): Unit =
def saveString(text: String, fileName: String, enc: String = "UTF-8", isOverwrite: Boolean = false): Unit =
val f = new java.io.File(fileName)

if !isOverwrite then
require(!f.exists(), s"The file $fileName already exists. To overwrite it, set isOverwrite=true.")

require(!f.isDirectory(), "The file you're trying to write to can't be a directory.")

val pw = new java.io.PrintWriter(f, enc)
try pw.write(text) finally pw.close()

Expand Down