Verifies a directory and checks to see if it's writeable or not if configured @param dir The path to check on @param need_write Set to true if the path needs write access @param create Set to true if the directory should be created if it does not exist @throws IllegalArgumentException if th
(final String dir,
final boolean need_write, final boolean create)
| 27 | * be written to |
| 28 | */ |
| 29 | public static void checkDirectory(final String dir, |
| 30 | final boolean need_write, final boolean create) { |
| 31 | if (dir.isEmpty()) |
| 32 | throw new IllegalArgumentException("Directory path is empty"); |
| 33 | final File f = new File(dir); |
| 34 | if (!f.exists() && !(create && f.mkdirs())) { |
| 35 | throw new IllegalArgumentException("No such directory [" + dir + "]"); |
| 36 | } else if (!f.isDirectory()) { |
| 37 | throw new IllegalArgumentException("Not a directory [" + dir + "]"); |
| 38 | } else if (need_write && !f.canWrite()) { |
| 39 | throw new IllegalArgumentException("Cannot write to directory [" + dir |
| 40 | + "]"); |
| 41 | } |
| 42 | } |
| 43 | } |