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 android.os.Environment;
import java.io.*;

public class Main {
    /**
     * Path to storage folder. Edit this params to change working files default location.
     * By default, the folder is set to external storage
     */
    public static String DIRECTORY_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
    /**
     * set to true if user external storage - false to user internal storage
     * default is true
     */
    public static boolean EXTERNAL_USE = false;

    /**
     * Clear old data and inserts new data into file.
     * @param filename
     * @param data
     * @return <c>true</c>, if data to file was inserted, <c>false</c> otherwise.</returns>
     */
    public static boolean InsertDataToFile(String filename, String data) {
        // get file paht
        String filePath = generateFilePath(filename);
        try {
            // open file to write
            FileOutputStream fos = new Activity().openFileOutput(filePath, Context.MODE_PRIVATE);
            // write data to file
            fos.write(data.getBytes());
            // close file
            fos.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return true;
    }

    /**
     * generate file path
     * @param filename
     * @return file path
     */
    public static String generateFilePath(String filename) {
        // if using external storage
        if (EXTERNAL_USE)
            return DIRECTORY_PATH + "/" + filename;
        // if usesing internal storage
        else
            return filename;
    }
}