Here you can find the source of createOrGetDir(String path, boolean mustBeReadable, boolean mustBeWritable)
Parameter | Description |
---|---|
path | the directory to be created or gotten |
mustBeReadable | whether to check for reading permission |
mustBeWritable | whether to check for writing permission |
Parameter | Description |
---|---|
IOException | if the directory cannot be created or permissions dont match |
public static File createOrGetDir(String path, boolean mustBeReadable, boolean mustBeWritable) throws IOException
/*/*from www .j a v a 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{ /** * Creates a directory or gets it if it exists. Can check for reading and writing permission. * Can take a directory of any depth even if none exists: * example /var/local/doesntExist1/doesntExist2/doesntExist3 * * @param path the directory to be created or gotten * @param mustBeReadable whether to check for reading permission * @param mustBeWritable whether to check for writing permission * @return a File representing that directory * * @throws IOException if the directory cannot be created or permissions dont match */ public static File createOrGetDir(String path, boolean mustBeReadable, boolean mustBeWritable) throws IOException { File dir = new File(path); createIfDoesntExist(dir, mustBeReadable, mustBeWritable); return dir; } /** * 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()); } }