我想創建一個文件夾,如果它不存在,然後從另一個文件夾複製消息到目標文件夾。我發現了一些我無法理解的奇怪行爲。鑑於以下摘錄:爲什麼成功創建文件夾後會出現FolderNotFoundException?
// messages is an array of Message instances.
// Source is the source folder
// destination is a string of the destination folder.
Folder dest = null;
try {
dest = store.getFolder(destination);
if (!dest.exists()) {
dest.create(Folder.HOLDS_MESSAGES | Folder.HOLDS_FOLDERS);
// Since folder's are not meant to cache I thought I'd get it again
// though this does not work either.
//dest.close(false);
//dest = store.getFolder(destination);
}
dest.open(Folder.READ_WRITE);
// Fails here
source.copyMessages(messages, dest);
source.setFlags(messages, new Flags(Flags.Flag.DELETED), true);
} catch (MessagingException ex) {
throw new MailProcessorException(ex.getMessage(), ex);
} finally {
if (dest != null) {
try {
dest.close(false);
} catch (MessagingException ex) {
System.err.println("Couldn't close destination folder.");
}
}
}
以下行爲被檢查:
- 如果該文件夾不存在:
- 該文件夾被創建
- 一個例外是在
source.copyMessages
拋出。
- 如果該文件夾確實存在:
- 這些消息如預期被複制。
- 消息標記爲刪除。
,也試圖與1.6.5。
在創建文件夾後嘗試將if(!dest.exists())'改爲'while(!dest.exists())'並添加Thread.sleep(1000)' – goravine 2013-02-11 02:40:44
否這不起作用。我之前在'create()'後面加了一個調試語句來輸出'dest.exists()',它總是會輸出'true'。 – 2013-02-11 03:04:42