Example usage for java.lang SecurityManager checkDelete

List of usage examples for java.lang SecurityManager checkDelete

Introduction

In this page you can find the example usage for java.lang SecurityManager checkDelete.

Prototype

public void checkDelete(String file) 

Source Link

Document

Throws a SecurityException if the calling thread is not allowed to delete the specified file.

Usage

From source file:Main.java

public static void main(String[] args) {
    System.setProperty("java.security.policy", "file:/C:/java.policy");

    SecurityManager sm = new SecurityManager();

    System.setSecurityManager(sm);

    // perform the check
    sm.checkDelete("test.txt");

    System.out.println("Allowed!");
}

From source file:Main.java

public static boolean deleteFile(String fileName) {
    boolean status;
    SecurityManager checker = new SecurityManager();

    if (!fileName.equals("")) {

        File path = Environment.getExternalStorageDirectory();
        File newPath = new File(path.toString() + fileName);
        checker.checkDelete(newPath.toString());
        if (newPath.isFile()) {
            try {
                Log.i("DirectoryManager deleteFile", fileName);
                newPath.delete();//from   w  w w  .  j a v a 2s  . c  om
                status = true;
            } catch (SecurityException se) {
                se.printStackTrace();
                status = false;
            }
        } else
            status = false;
    } else
        status = false;
    return status;
}

From source file:Main.java

public static boolean deleteDirectory(String fileName) {
    boolean status;
    SecurityManager checker = new SecurityManager();

    if (!fileName.equals("")) {

        File path = Environment.getExternalStorageDirectory();
        File newPath = new File(path.toString() + fileName);
        checker.checkDelete(newPath.toString());
        if (newPath.isDirectory()) {
            String[] listfile = newPath.list();
            // delete all files within the specified directory and then
            // delete the directory
            try {
                for (int i = 0; i < listfile.length; i++) {
                    File deletedFile = new File(newPath.toString() + "/" + listfile[i].toString());
                    deletedFile.delete();
                }//w  w w  .  j  a  v a2 s .  c o m
                newPath.delete();
                Log.i("DirectoryManager deleteDirectory", fileName);
                status = true;
            } catch (Exception e) {
                e.printStackTrace();
                status = false;
            }

        } else
            status = false;
    } else
        status = false;
    return status;
}

From source file:Main.java

public static boolean deleteDirectory(String fileName) {
    boolean status;
    SecurityManager checker = new SecurityManager();

    if (!fileName.equals("")) {

        File path = Environment.getExternalStorageDirectory();
        File newPath = new File(path.toString() + fileName);
        checker.checkDelete(newPath.toString());
        if (newPath.isDirectory()) {
            String[] listfile = newPath.list();
            // delete all files within the specified directory and then
            // delete the directory
            try {
                for (int i = 0; i < listfile.length; i++) {
                    File deletedFile = new File(newPath.toString() + "/" + listfile[i].toString());
                    deletedFile.delete();
                }// ww  w.j a va  2  s.c  o m
                newPath.delete();
                Log.d(TAG, "DirectoryManager deleteDirectory" + fileName);
                status = true;
            } catch (Exception e) {
                e.printStackTrace();
                status = false;
            }

        } else
            status = false;
    } else
        status = false;
    return status;
}

From source file:com.phonegap.DirectoryManager.java

/**
 * Delete file./*w  ww.j a  va  2s .c om*/
 * 
 * @param fileName            The name of the file to delete
 * @return                  T=deleted, F=not deleted
 */
protected static boolean deleteFile(String fileName) {
    boolean status;
    SecurityManager checker = new SecurityManager();

    // Make sure SD card exists
    if ((testSaveLocationExists()) && (!fileName.equals(""))) {
        File path = Environment.getExternalStorageDirectory();
        File newPath = constructFilePaths(path.toString(), fileName);
        checker.checkDelete(newPath.toString());

        // If file to delete is really a file
        if (newPath.isFile()) {
            try {
                Log.i("DirectoryManager deleteFile", fileName);
                newPath.delete();
                status = true;
            } catch (SecurityException se) {
                se.printStackTrace();
                status = false;
            }
        }
        // If not a file, then error
        else {
            status = false;
        }
    }

    // If no SD card
    else {
        status = false;
    }
    return status;
}

From source file:com.phonegap.DirectoryManager.java

/**
 * Delete directory./* w  w  w  .j a  v a2  s . c o m*/
 * 
 * @param fileName      The name of the directory to delete
 * @return            T=deleted, F=could not delete
 */
protected static boolean deleteDirectory(String fileName) {
    boolean status;
    SecurityManager checker = new SecurityManager();

    // Make sure SD card exists
    if ((testSaveLocationExists()) && (!fileName.equals(""))) {
        File path = Environment.getExternalStorageDirectory();
        File newPath = constructFilePaths(path.toString(), fileName);
        checker.checkDelete(newPath.toString());

        // If dir to delete is really a directory
        if (newPath.isDirectory()) {
            String[] listfile = newPath.list();

            // Delete all files within the specified directory and then delete the directory
            try {
                for (int i = 0; i < listfile.length; i++) {
                    File deletedFile = new File(newPath.toString() + "/" + listfile[i].toString());
                    deletedFile.delete();
                }
                newPath.delete();
                Log.i("DirectoryManager deleteDirectory", fileName);
                status = true;
            } catch (Exception e) {
                e.printStackTrace();
                status = false;
            }
        }

        // If dir not a directory, then error
        else {
            status = false;
        }
    }

    // If no SD card 
    else {
        status = false;
    }
    return status;
}