I've been attempting (unsuccessfully) to write multiple lines of data retrieved from Firebase to a file, which I will then upload to storage. I created a PrintWriter using this example and then used a loop to gather values from multiple children in my Firebase Database. The filename has a .csv extension.
public PrintWriter pw;
...
File file = new File(this.getFilesDir(), filename);
pw = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
String columns = <COLUMNS>;
pw.write(columns);
patientRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
Object object = postSnapshot.getValue(Object.class);
String value1 = object.getValue1();
...
String row = value1 + ", " + value2 + ", "...;
pw.println(row);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
pw.close();
From a Log statement, I can see that the string I create from the values in onDataChanged() is correct. My only problem is that it won't write to the file from the loop. When the CSV file is uploaded, the only text in the file is the columns, plus a blank space underneath.
Why aren't the rows being appended?