Android Directory Create createIfDoesntExist(File dir, boolean mustBeReadable, boolean mustBeWritable)

Here you can find the source of createIfDoesntExist(File dir, boolean mustBeReadable, boolean mustBeWritable)

Description

creates a directory if it doesnt exist and verifies permissions

License

Apache License

Parameter

Parameter Description
dir a parameter
mustBeReadable a parameter
mustBeWritable a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void createIfDoesntExist(File dir,
        boolean mustBeReadable, boolean mustBeWritable)
        throws IOException 

Method Source Code

/*/* w w  w.  j  a va 2s  .com*/
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{
    /**
     * creates a directory if it doesnt exist and verifies permissions
     * @param dir
     * @param mustBeReadable
     * @param mustBeWritable
     * @throws IOException
     */
    public static void createIfDoesntExist(File dir,
            boolean mustBeReadable, boolean mustBeWritable)
            throws IOException {
        if (dir.exists()) {
            if (!dir.isDirectory())
                throw new IOException(dir.getAbsolutePath()
                        + " exists but isn't a directory");
        } else {
            if (!dir.mkdirs())
                throw new IOException("Couldn't create directory "
                        + dir.getAbsolutePath());
        }

        if (mustBeReadable && !dir.canRead())
            throw new IOException("Cannot read from "
                    + dir.getAbsolutePath());
        if (mustBeWritable && !dir.canWrite())
            throw new IOException("Cannot write in "
                    + dir.getAbsolutePath());
    }
}

Related

  1. createDirIfNotExists(File d)
  2. createDirIfNotExists(String d)
  3. createDirStructureForFile(File file)
  4. createDirs(String dir, boolean ignoreIfExitst)
  5. createDirsForFile(File file)
  6. createMissingParentDirectories(File file)
  7. createNewDirectory(String directorPath)
  8. createOrGetDir(String path, boolean mustBeReadable, boolean mustBeWritable)
  9. dirWritable(String dir)