I am writing a code in Android Studio and I want to save the user input .txt/.doc file in a folder that I have created in Downloads folder(MyFolder), but I cannot manage to do that. I have all the permissions to the External Storage. I know that I have to use FileOutputStream instead of openFileOutput, but I do not know how.
This is my piece of code:
String text1 = "Name: " + name.getText().toString();
String FILE_NAME = "The name is: " + name.getText().toString() + ".doc";
FileOutputStream fos = null;
File makedir = new File(Environment.getExternalStorageDirectory() + "/Download/MyFolder");
makedir.mkdirs();
File file = new File(makedir, FILE_NAME);
try {
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(text1.getBytes());
fos.write("\n".getBytes());
name.getText().clear();
Toast.makeText(this, "File Saved!",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}