First of all, you are missing a pair of braces around your function. I think, you intended it to look like this:
def write(str:String, file:String):Path = {
Paths.get(file)
p.write(str,file)
}
It looks like it, because str
and file
are parameters to the function, and you are trying to use them outside of it (a function body without braces is just one statement).
Now, the way I "fixed" it for you, still doesn't make very much sense.
First, Paths.get(file)
doesn't do anything, it just returns the Path
object, which you do not assign to anything, so, this call does not have any effect. Second, Path
does not have a method, called write
, so the second line isn't going to work. Perhaps, you intended it to implicitly end up calling PathImplicits.write
, but that won't work (you'd have to be outside of that class), and that's a good thing, because you are actually inside that function, and, if that line called it again, you'd get into infinite recursion.
Let's break your problem into two parts. First, let's forget about implicits and other fancy stuff, and just figure out how to write a string into a file. There is a whole lot of different ways to do it. Here is one for instance:
def writeToFile(file: File, str: String): Unit = {
val writer = new FileWriter(file)
try { writer.append(str).append("\n") }
finally { writer.close }
}
Now, if you want to make it work with Path
implicitly, you'd need something like this:
object PathImplicits {
implicit class RichPath(p: Path) extends AnyVal {
def write(str: String) = writeToFile(p.toFile, str)
}
}
That's it. Now, you should be able to write something like this:
import PathImplicits._
Paths.get("/some/path/to/file.txt").write("Foo!")