0

I'm having trouble with writing to a file. Here is my code:

File("test.txt").printWriter().use { out ->
            out.println("First line")
            out.println("Second line")

I've looked that the answers at How do I write to a file in Kotlin? but they don't work; gradle builds and runs but test.txt stays empty. I've also checked the Kotlin documentation but I can't find anything.

Thanks!

Edit: If I put the full path of the file then it works.

  • 2
    This works fine for me, what's your root directory, can you find it using `println(File("test.txt").path)`? – Animesh Sahu Feb 16 '20 at 08:33
  • 1
    Regarding your edit, writing to `File("test.txt")` will write to a file located at `/test.txt` (see https://en.wikipedia.org/wiki/Working_directory). Is that the file you were looking at? Is that the same path used when you manually enter the absolute path? – Slaw Feb 17 '20 at 06:19

0 Answers0