Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;

import android.os.Environment;

public class Main {
    private static String APP_DIR = "java2s";
    public static final File EXTERNAL_STORAGE_DIRECTORY = Environment.getExternalStorageDirectory();

    public static boolean delFile(String appath, String filename) {
        if (checkDir(appath) == false) {
            return false;
        }
        File file = getFile(appath, filename);
        try {
            if (file.exists()) {
                return file.delete();
            } else {
                return false;
            }
        } catch (Exception ex) {
            return false;
        }
    }

    public static boolean delFile(String file) {
        return delFile(null, file);
    }

    public static boolean checkDir(String appath) {
        String dir = getPath(appath);

        if (checkSD() == false) {
            return false;
        }
        File tf = new File(dir);

        if (tf.exists()) {
            return true;
        }

        boolean ret = tf.mkdirs();
        if (ret == false) {
            return false;
        }

        return true;
    }

    public static File getFile(String appath, String filename) {
        if (checkDir(appath) == false) {
            return null;
        }

        try {
            String file = getFilePath(appath, filename);
            File fileObj = new File(file);
            return fileObj;
        } catch (SecurityException ex) {
            return null;
        }
    }

    public static File getFile(String file) {
        return getFile(null, file);
    }

    public static String getPath(String appath) {
        String path = null;
        if (appath != null) {
            path = EXTERNAL_STORAGE_DIRECTORY + "/" + APP_DIR + "/" + appath + "/";
        } else {
            path = EXTERNAL_STORAGE_DIRECTORY + "/" + APP_DIR + "/";
        }
        return path;
    }

    public static boolean checkSD() {
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    public static String getFilePath(String appath, String filename) {
        String file = null;
        if (appath != null) {
            file = EXTERNAL_STORAGE_DIRECTORY + "/" + APP_DIR + "/" + appath + "/" + filename;
        } else {
            file = EXTERNAL_STORAGE_DIRECTORY + "/" + APP_DIR + "/" + filename;
        }
        return file;
    }

    public static String getFilePath(String file) {
        return getFilePath(null, file);
    }
}