I tried to save a text file to internal storage of the app but it does not work
Code is
val defFileName = "text.def"
val ioFile = File(defFileName)
ioFile.createnewFile()
ioFile.writeText("Hello World")
I have tried different code versions to achive it but every time I got
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.io.FileNotFoundException: hello_file: open failed: EROFS (Read-only file system)
EROFS ReadOnlyFileSystem
Any help?
Edit:
I get the path with
mContext.applicationInfo.dataDir.toString()
This code runs without error and ioDefFile ="xxx.def"
val fileOutputStream:FileOutputStream
try {
fileOutputStream = context.openFileOutput(ioDefFile, Context.MODE_PRIVATE)
fileOutputStream.write("just a textString".toByteArray())
}catch (e: Exception){
e.printStackTrace()
}
This code says -> open failed: EROFS (Read-only file system)
try{
val fileWriter = FileWriter(ioDefFile)
for (x in 0..(DSSize-1)){
fileWriter.write(dl[x]+ "\n")
}
fileWriter.close()
} catch (exception: Exception){
println(exception.message)
}
Did not see the error in Code 2 - it did not work with oder without adding the path
Now it works :)
val foStream: FileOutputStream
try {
foStream = context.openFileOutput(ioDefFile, Context.MODE_PRIVATE)
for (x in 0..(DSSize - 1)) {
foStream.bufferedWriter().write(dl[x])
foStream.bufferedWriter().newLine()
}
} catch (e: Exception) {
e.printStackTrace()
}