我想创建一个文件夹,如果它不存在,然后从另一个文件夹复制消息到目标文件夹。我发现了一些我无法理解的奇怪行为。鉴于以下摘录:为什么成功创建文件夹后会出现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