Example usage for org.apache.cordova.file FileExistsException FileExistsException

List of usage examples for org.apache.cordova.file FileExistsException FileExistsException

Introduction

In this page you can find the example usage for org.apache.cordova.file FileExistsException FileExistsException.

Prototype

public FileExistsException(String msg) 

Source Link

Usage

From source file:com.MustacheMonitor.MustacheMonitor.FileUtils.java

License:Apache License

/**
 * Loops through a directory deleting all the files.
 *
 * @param directory to be removed//w  w w. j a  va 2  s.  c om
 * @return a boolean representing success of failure
 * @throws FileExistsException
 */
private boolean removeDirRecursively(File directory) throws FileExistsException {
    if (directory.isDirectory()) {
        for (File file : directory.listFiles()) {
            removeDirRecursively(file);
        }
    }

    if (!directory.delete()) {
        throw new FileExistsException("could not delete: " + directory.getName());
    } else {
        return true;
    }
}

From source file:com.MustacheMonitor.MustacheMonitor.FileUtils.java

License:Apache License

/**
 * Creates or looks up a file.//from  ww w  .ja  v a 2 s  . c  o  m
 *
 * @param dirPath base directory
 * @param fileName file/directory to lookup or create
 * @param options specify whether to create or not
 * @param directory if true look up directory, if false look up file
 * @return a Entry object
 * @throws FileExistsException
 * @throws IOException
 * @throws TypeMismatchException
 * @throws EncodingException
 * @throws JSONException
 */
private JSONObject getFile(String dirPath, String fileName, JSONObject options, boolean directory)
        throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException {
    boolean create = false;
    boolean exclusive = false;
    if (options != null) {
        create = options.optBoolean("create");
        if (create) {
            exclusive = options.optBoolean("exclusive");
        }
    }

    // Check for a ":" character in the file to line up with BB and iOS
    if (fileName.contains(":")) {
        throw new EncodingException("This file has a : in it's name");
    }

    File fp = createFileObject(dirPath, fileName);

    if (create) {
        if (exclusive && fp.exists()) {
            throw new FileExistsException("create/exclusive fails");
        }
        if (directory) {
            fp.mkdir();
        } else {
            fp.createNewFile();
        }
        if (!fp.exists()) {
            throw new FileExistsException("create fails");
        }
    } else {
        if (!fp.exists()) {
            throw new FileNotFoundException("path does not exist");
        }
        if (directory) {
            if (fp.isFile()) {
                throw new TypeMismatchException("path doesn't exist or is file");
            }
        } else {
            if (fp.isDirectory()) {
                throw new TypeMismatchException("path doesn't exist or is directory");
            }
        }
    }

    // Return the directory
    return getEntry(fp);
}