Well apparently i suck at okio. whats the proper w...
# squarelibraries
c
Well apparently i suck at okio. whats the proper way to create a directory + file?
Copy code
@Throws(IOException::class)
fun writeStringToFile(path: Path, myMessage: String) {
  FileSystem.SYSTEM.createDirectories(path)

  FileSystem.SYSTEM.write(path) {
      writeUtf8(myMessage)
  }
}
Calling code
Copy code
writeStringToFile("mydir/myMessage.json".toPath(), "hello, moto")
Keep getting
java.io.IOException: failed to create directory: mydir
edit: maybe cuz im on an emu 🤔
e
Android?
c
Yeah
I don't work with files directly much so im probably missing something basic here. but i thought id be able to write files in my apps sandbox without issue
e
Is
mydir
just a placeholder? You should be using something like https://developer.android.com/reference/android/content/Context#getFilesDir() to get a directory that you can write to
c
oh. I thought FileSystem.SYSTEM would point to getFileDir
Copy code
writeStringToFile("${context.filesDir}/mydir/myfile.json".toPath(), "hello, moto")
going to give that a shot 🤞
Had to do this apparently
Copy code
@Throws(IOException::class)
fun writeStringToFile(pathWithFileName: Path, myMessage: String) {
  FileSystem.SYSTEM.createDirectories(pathWithFileName.parent!!)

  FileSystem.SYSTEM.write(pathWithFileName) {
      writeUtf8(myMessage)
  }
}
e
🙈 I always forget about that. I usually declare my files like:
Copy code
val someFile = File(parent, "foo/bar/baz").apply { parent.mkdirs() }
Aside from that, did you still have to use
context.filesDir
?
j
There's not much reason to use Okio here
b
other sidenote: injecting the filesystem will allow you to write tests sweet and easy
j
not if you bottom out in AtomicFile which only works on `File`s
c
Oh hm. So instead of okio + moshi + atomicFile I'll just use moshi + atomicFile
I've got okio + moshi working now which is nice though to save to disk. I tried getting atomicFile involved but couldn't figure out how to use it, but sounds like Jake says that's not possible
j
i still think you should try datastore again
135 Views