Locates the given dirName as a full path, in the current directory or in successively higher parent directory above. @param dirName @return @throws IOException
(String dirName)
| 191 | * @throws IOException |
| 192 | */ |
| 193 | public static String locateDirectory(String dirName) throws IOException { |
| 194 | if (Utils.dirExists(dirName)) return dirName; |
| 195 | |
| 196 | String path = null; |
| 197 | String curDir = new File(".").getCanonicalPath(); |
| 198 | |
| 199 | // Go for at most 10 levels up |
| 200 | for (int i = 0; i < 10; i++) { |
| 201 | path = String.format("%s/%s", curDir, dirName); |
| 202 | if (Utils.dirExists(path)) break; |
| 203 | curDir = String.format("%s/..", curDir); |
| 204 | } |
| 205 | |
| 206 | if (path != null) { |
| 207 | File file = new File(path); |
| 208 | path = file.getCanonicalPath(); |
| 209 | if (!Utils.dirExists(path)) path = null; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | return path; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Same as locateDirectory(dirName), but also creates it if it doesn't exist. |
no test coverage detected