Arun nagar
Arun nagar

Reputation: 176

java program cannot create file folder in the system.why?

I am working on project and in which I make a program to create a file folder in system. When i run my program it shows:

java.io.FileNotFoundException:File 'E:\Program Files\IBM\SDP\Profin\EmailAttachment\30189\31609\T0000021811.pdf' does not exist

My code is:

    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;        
}

Upvotes: 0

Views: 522

Answers (2)

Jan
Jan

Reputation: 13858

The code you have performs a

FileUtils.readFileToByteArray(somePath);

Obviously, that File is just not there. So most likely the process that was required to write that file to that location did not work.

You have to decide what to do in these cases. Currently you fail the whole attachment-generation stuff. Maybe you'd be better of adding a default attachment in this case with some text "Attachment data not found in storage" or something.

Upvotes: 0

Akshay Gehi
Akshay Gehi

Reputation: 362

You need to make sure that the parent folder exists where you want to create the file

You can use File(parent).mkdirs() before trying to write your file

Upvotes: 1

Related Questions