Good job on figuring it out.
Just like to give some advice. I noticed you made a common mistake that new developers make(not a bad one). Your code should be in 3 separate methods to make your code modular and reusable. Remember java is an oop language(object oriented). So you want to make your objects reusable.
By having your read file method in its own object that accepts a variable and returns a string, you can reuse the read method anywhere in your app. Same goes for the write to file method. Then your button should just pass the variable to the method.
Here's an example of a reusable method I use to read a file which I can reuse anywhere as many times in my app. (keep in mind I wrote this for a desktop app, but should fit right in on android)
Java:
public static String getFileContent(String file) {
String content = "";
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
if (line != null) {
sb.append("\n");
}
}
content = sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
Now I can get contents of any file through this one object like so...
Java:
getFileContent("/path/to/filename");
Now lets say you have a separate write to file method, then passing the variable would look something like this assuming the method is called writeToFile()...
Java:
writeToFile(getFileContent("/path/to/filename"));
I hope this sample helps you or anyone else think about breaking their code into smaller reusable objects rather than writing the same code over and over.
Cheers