| | 252 | |
|---|
| | 253 | public static void renameFile(String oldname, String newname) |
|---|
| | 254 | { |
|---|
| | 255 | File file = new File(oldname); |
|---|
| | 256 | |
|---|
| | 257 | if (!file.exists()) |
|---|
| | 258 | { |
|---|
| | 259 | log.error("Rename failed. File does not exist."); |
|---|
| | 260 | return; |
|---|
| | 261 | } |
|---|
| | 262 | |
|---|
| | 263 | if (!file.canWrite()) |
|---|
| | 264 | { |
|---|
| | 265 | log.error("Rename failed. File is not writable."); |
|---|
| | 266 | return; |
|---|
| | 267 | } |
|---|
| | 268 | |
|---|
| | 269 | // If it is a directory, make sure it is empty |
|---|
| | 270 | if (file.isDirectory()) |
|---|
| | 271 | { |
|---|
| | 272 | String[] files = file.list(); |
|---|
| | 273 | if (files.length > 0) |
|---|
| | 274 | { |
|---|
| | 275 | log.error("Rename failed. Directory is not empty."); |
|---|
| | 276 | return; |
|---|
| | 277 | } |
|---|
| | 278 | } |
|---|
| | 279 | |
|---|
| | 280 | // Attempt to rename it |
|---|
| | 281 | boolean success = file.renameTo(new File(newname)); |
|---|
| | 282 | |
|---|
| | 283 | if (!success) |
|---|
| | 284 | { |
|---|
| | 285 | //try garbage collection to release locks on file |
|---|
| | 286 | System.gc(); |
|---|
| | 287 | // Attempt to rename again |
|---|
| | 288 | success = file.renameTo(new File(newname)); |
|---|
| | 289 | |
|---|
| | 290 | if(!success) |
|---|
| | 291 | { |
|---|
| | 292 | log.error("Rename failed. Could not rename file."); |
|---|
| | 293 | } |
|---|
| | 294 | } |
|---|
| | 295 | } |
|---|