Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.app.Activity;
import android.content.Context;

import java.io.File;
import java.io.FileOutputStream;

public class Main {
    public static void saveJsonToFile(String toSave, String filename, Activity currActivity) {

        // check if the file exists
        File dir = currActivity.getFilesDir();
        File file = new File(dir, filename);

        if (file.exists()) {
            file.delete();
        }

        FileOutputStream outputStream;
        try {
            outputStream = currActivity.openFileOutput(filename, Context.MODE_APPEND);
            outputStream.write(toSave.getBytes());
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}