Example usage for java.io FileWriter toString

List of usage examples for java.io FileWriter toString

Introduction

In this page you can find the example usage for java.io FileWriter toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:nz.ac.auckland.lablet.script.ScriptRunnerActivity.java

/**
 * Extracts the user answers from a bundle and saves them to a file.
 *
 * @param bundle the top-level (script-level) bundle of data
 * @param fileWriter the FileWriter object in which to write user answers
 * @return true if everything is written successfully; false otherwise
 *///from w w  w  .  j  a va  2  s .c  o m
private boolean saveUserAnswersToFile(@Nullable Bundle bundle, @Nullable FileWriter fileWriter) {
    if (bundle == null || fileWriter == null) {
        return false;
    }

    try {
        // write timestamp and script name to file
        fileWriter.write((new Date()).toString() + "\n");
        fileWriter.write(bundle.getString("script_name", "unknown_script") + "\n\n");

        int i = 0;
        Bundle sheet;
        Bundle child;

        // for all sheets in bundle ("0", "1", "2", ...)
        while ((sheet = bundle.getBundle(Integer.toString(i++))) != null) {
            int j = 0;
            int k = 0;
            boolean sheet_label_printed = false;

            // for all child objects ("child0", "child1", "child2", ...)
            while ((child = sheet.getBundle("child" + Integer.toString(j++))) != null) {
                String question;
                String answer;

                // if child has a "question" and "answer" field
                if ((question = child.getString("question")) != null
                        && (answer = child.getString("answer")) != null) {

                    // print sheet title if not printed yet
                    if (!sheet_label_printed) {
                        fileWriter.write("Sheet" + Integer.toString(i - 1) + "\n--------\n");
                        sheet_label_printed = true;
                    }

                    // print question
                    fileWriter.write(String.format(Locale.US, "Q%d: %s\n", ++k, question));

                    // print answer
                    fileWriter.write(String.format(Locale.US, "%s\n\n", answer));
                }
            }
        }
    } catch (IOException e) {
        // ERROR
        Log.e(TAG, "Error: ", e);
        Log.e(TAG, "Could not write user answers to file: " + fileWriter.toString());
        try {
            fileWriter.close();
        } catch (IOException e1) {
            Log.w(TAG, "Error: ", e1);
            Log.w(TAG, "Could not close user answer file: " + fileWriter.toString());
        }
        return false;
    }
    try {
        // flush and close file with user answers
        fileWriter.close();
    } catch (IOException e) {
        Log.w(TAG, "Error: ", e);
        Log.w(TAG, "Could not close user answer file: " + fileWriter.toString());
    }

    // SUCCESS
    Log.i(TAG, "User answer saved successfully to: " + fileWriter.toString());
    return true;
}