Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.os.Environment;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

public class Main {
    private static final String DIR_BOARDS_EXTERNAL = "GameOfLife";

    public static JSONObject getBoardJSONFromFile(String pathNameFile) {
        File file = new File(getDirectoryBoards(), pathNameFile);
        if (!file.exists()) {
            return null;
        }

        String output = loadBoard(file);
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(output);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jsonObject;
    }

    private static File getDirectoryBoards() {
        File file = new File(Environment.getExternalStorageDirectory(),
                DIR_BOARDS_EXTERNAL);
        if (!file.exists()) {
            file.mkdir();
        }

        return file;
    }

    private static String loadBoard(File file) {
        String output = null;
        try {
            FileInputStream is = new FileInputStream(file);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            output = new String(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return output;
    }
}


d