EDIT: While following up on the two suggestions I determined that the following would work for writing the Strings to a file as Byte arrays. But now I need to figure out how to read the Byte arrays back into Strings. I could use help with that please... Is there a FileInputStream component that is the reverse equivalent of this FileOutputStream? This works for writing the Strings as Byte arrays.
try {
FileOutputStream fos = openFileOutput("stack.txt", Context.MODE_PRIVATE);
for (String item : stack)
fos.write(stack.pop().getBytes());
fos.close();
sb.append("The Stack is saved.\n");
} catch (IOException e) {
sb.append(e.toString());
}
displayText(sb.toString());
I am trying to write Strings from an ArrayDeque to a file. I am using the ArrayDeque as a Stack. The strings are of varying length. I get this error on opening the file for output...
java.io.FileNotFoundException: stack.txt: open failed: EROFS (Read-only file system)
I think I have seen all of the posts on this topic but with no help. My two code snippets are:
- In onCreate()...
root = new File(getExternalFilesDir(null), "/HTMLSpyII/");
if (!root.exists()) {
if (!root.mkdirs()) {
runTimeAlert("File path does not exist or\n" +
"Unable to create path \n" + root.toString());
}
}
- In the program...
try {
DataOutputStream dos = new DataOutputStream(
new FileOutputStream("stack.txt")); // program fails here
for (String item : stack)
dos.writeUTF(stack.pop());
dos.close();
sb.append("The Stack is saved.\n");
} catch (IOException e) {
sb.append(e.toString());
}
displayText(sb.toString());
I have parallel code for reading them back using DataInputStream with FileInputStream. I am missing something. Is it perhaps some initial preparation before creating the file?
Doing some research I have a feeling that the problem may be that I am not yet familiar with the requirements for using the new app-specific, persistent, internal/external storage? But I do not know so I am looking for some guidance, please :)