Android File Exist getExistingFile(String path, boolean mustBeReadable, boolean mustBeWritable, boolean isDirectory)

Here you can find the source of getExistingFile(String path, boolean mustBeReadable, boolean mustBeWritable, boolean isDirectory)

Description

gets a file that exists and matches the requested permissions or throws an exception checks if it s a file or a directory

License

Apache License

Parameter

Parameter Description
path the file path
mustBeReadable whether to check for reading permission
mustBeWritable whether to check for writing permission
isDirectory true if it should be a directory, false if a file

Return

a File representing that file

Declaration

public static File getExistingFile(String path, boolean mustBeReadable,
        boolean mustBeWritable, boolean isDirectory) throws IOException 

Method Source Code

/*//from w  w w.  j  a  va  2 s  . co m
Copyright 2008 Flaptor (flaptor.com) 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

    http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import org.apache.log4j.Logger;

public class Main{
    /**
     * gets a file that exists and matches the requested permissions or throws an exception
     * checks if it s a file or a directory
     * 
     * @param path the file path
     * @param mustBeReadable whether to check for reading permission
     * @param mustBeWritable whether to check for writing permission
     * @param isDirectory true if it should be a directory, false if a file
     * @return a File representing that file 
     */
    public static File getExistingFile(String path, boolean mustBeReadable,
            boolean mustBeWritable, boolean isDirectory) throws IOException {
        File file = getExistingFile(path, mustBeReadable, mustBeWritable);
        if (file.isDirectory() && !isDirectory)
            throw new IOException(file.getAbsolutePath()
                    + " exists but is not a file");
        if (!file.isDirectory() && isDirectory)
            throw new IOException(file.getAbsolutePath()
                    + " exists but is not a directory");
        return file;
    }
    /**
     * gets a file that exists and matches the requested permissions or throws an exception
     * (can be a file or a directory)
     * 
     * @param path the file path
     * @param mustBeReadable whether to check for reading permission
     * @param mustBeWritable whether to check for writing permission
     * @return a File representing that file 
     */
    public static File getExistingFile(String path, boolean mustBeReadable,
            boolean mustBeWritable) throws IOException {
        File file = new File(path);
        if (!file.exists())
            throw new FileNotFoundException("file not found: "
                    + file.getAbsolutePath());
        if (mustBeReadable && !file.canRead())
            throw new IOException("Cannot read from "
                    + file.getAbsolutePath());
        if (mustBeWritable && !file.canWrite())
            throw new IOException("Cannot write in "
                    + file.getAbsolutePath());
        return file;
    }
}

Related

  1. exists(String parent, String fileName)
  2. exists(String path)
  3. exists(String path)
  4. fileExists(String path)
  5. fileOrPathExists(String path)
  6. isExist(String path)
  7. isFileExist(String fileName)
  8. isFileExist(String fileName)
  9. isFileExist(String folder, String fileName)