Writing some text to a file on an android device seems to be a major endeavor. Starting from an answer given here I have implemented the following code into my single 'Hello-World' activity:
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(this.openFileOutput("config.txt", Context.MODE_PRIVATE));
outputStreamWriter.write("lalala");
outputStreamWriter.close();
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
which does not throw an exception, but seems to work. But is there a way to 'see' the file created with the File Manager
on android? The code snippet seems to write to a 'secret' location on the android file system related to the application itself (governed by using this.openFileOutput
).
Consulting different google-links (this and this and this) I come up with the following code:
File file = new File(this.getExternalFilesDir("temp"), "testfile.txt");
FileOutputStream fileOutput = openFileOutput(file.getName(), Context.MODE_WORLD_WRITEABLE);
fileOutput.write("lalala");
fileOutput.close();
which throws an error
Error:(55, 19) error: no suitable method found for write(String) method FileOutputStream.write(int) is not applicable (actual argument String cannot be converted to int by method invocation conversion) method FileOutputStream.write(byte[],int,int) is not applicable (actual and formal argument lists differ in length) method OutputStream.write(int) is not applicable (actual argument String cannot be converted to int by method invocation conversion) method OutputStream.write(byte[],int,int) is not applicable (actual and formal argument lists differ in length) method OutputStream.write(byte[]) is not applicable (actual argument String cannot be converted to byte[] by method invocation conversion)
So how to do this right?
As a side note: This is for debugging/educational purposes only and not intended to be part of a final app!
To be clear: I want to create a file inside the temp
directory I can see with a FileManager (the temp
directory is on the same level as My Documents
, Music
, DCIM
, Download
and the like...)