0

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?

1 Answer 1

1
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();

You have added some event listener to patientRef but it doesn't mean that onDataChange(...) has been executed immediately, it's only registered as listener. After adding it you close the PrintWriter, so nothing except columns wouldn't been written because when SingleValueEvent would be executed pw is already closed. You need to close pw after actual event execution or inside it.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm so annoyed that I completely missed this. Tunnel vision I guess. Thanks so much! I'd upvote a million times if I could.
@Taras: your description is correct, but the code in your answer still closes the writer after attaching the listener. Did you mean to move pw.close() into onDataChange()?
@FrankvanPuffelen I just take the only part of author`s code which contains the error and described what is going wrong there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.