Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStreamWriter;
import android.content.Context;

public class Main {
    /**
     * Method - saveMessage
     * 
     * Description - Saves messages to the file by either appending or rewriting, used by any activity that needs to save the messages
     * 
     * @param ctx - Context of the Application that called the function
     * @param message - The messages to save to the file, most often a long String of several files
     * @param mode - Very likely either Context.MODE_APPEND or Context.MODE_PRIVATE for appending or rewriting respectively
     */

    public static void saveMessage(Context ctx, String message, int mode) {
        try {

            // Creating objects to write to the file
            FileOutputStream fos = ctx.openFileOutput("messages.dat", mode);
            OutputStreamWriter osw = new OutputStreamWriter(fos);

            osw.write(message); // Writing to the file
            osw.flush(); // Making sure all characters have been written

            // Closing objects after writing has finished
            osw.close();
            fos.close();

        } catch (FileNotFoundException e) {
            return;
        } catch (IOException e) {
            return;
        }
    }
}