For reading files in Scala, there is
Source.fromFile(\"file.txt\").mkString
Is there an equivalent and concise way to write a string to fi
I know it's not one line, but it solves the safety issues as far as I can tell;
// This possibly creates a FileWriter, but maybe not
val writer = Try(new FileWriter(new File("filename")))
// If it did, we use it to write the data and return it again
// If it didn't we skip the map and print the exception and return the original, just in-case it was actually .write() that failed
// Then we close the file
writer.map(w => {w.write("data"); w}).recoverWith{case e => {e.printStackTrace(); writer}}.map(_.close)
If you didn't care about the exception handling then you can write
writer.map(w => {w.writer("data"); w}).recoverWith{case _ => writer}.map(_.close)