我正在研究项目,并在其中创建一个程序来在系统中创建文件夹。当我运行程序时,它显示:
java.io.FileNotFoundException:文件'E:\ Program
Files \ IBM \ SDP \ Profin \ EmailAttachment \ 30189 \ 31609 \ T0000021811.pdf'确实
不存在
我的代码是:
public EmailQueueAttachment[] get(Long emailQueueId)throws InvalidDAOArgumentException {
if (emailQueueId == null) {
throw new InvalidDAOArgumentException("Email Queue Id can not be null.");
}
EmailQueueAttachmentListHelper criteria = new EmailQueueAttachmentListHelper();
criteria.setEmailQueueId(emailQueueId);
List<Model> emailQueueAttachmentList = getList(criteria, -1, -1).getCurrentPageData();
if (emailQueueAttachmentList != null) {
EmailQueueAttachment[] attachments = (EmailQueueAttachment[]) emailQueueAttachmentList.toArray(new EmailQueueAttachment[emailQueueAttachmentList.size()]);
for(int i=0; i<attachments.length; i++){
try {
attachments[i].setAttachmentContent(FileUtils.readFileToByteArray(new File(SystemUtil.getEmailAttachmentFolderName() + File.separator + emailQueueId + File.separator + attachments[i].getRecNo() + File.separator + attachments[i].getAttachmentName())));
} catch (IOException e) {
e.printStackTrace();
throw new DAOException(e);
}
}
return attachments;
}
return null;
}
最佳答案
您需要确保父文件夹存在于您要创建文件的位置
您可以在尝试写入文件之前使用File(parent).mkdirs()