0

Fixed: Instead of calling isFile() I used exists() and it seems to be working fine. If possible could someone explain why this change worked?

I'm attempting to write out to an excel file but am having a problem when trying to create that file if the name already exists.

Basically I am taking a file that is uploaded to a server, reading it, and then outputting a report file in a new location with the same filename. I tried to do this by simply checking if the file already existed and then adding a number onto the filename. My code works if the file doesn't exist or if it exists without a number (e.g. filename.xls). If a file exists with the name "filename1.xls" the server just seems to hang when trying to write the file. What can do to fix this?

Here is my code:

        String destination = "c:/apache-tomcat-7.0.8/webapps/reports/" + fileName.substring( fileName.lastIndexOf("\\")+1, fileName.lastIndexOf(".")) + ".xls";
        int filenum = 1;
        while (new File(destination).isFile()) {
            destination = "c:/apache-tomcat-7.0.8/webapps/reports/" + fileName.substring( fileName.lastIndexOf("\\")+1, fileName.lastIndexOf(".")) + filenum + ".xls";
            filenum++;
        }
        WritableWorkbook workbook = Workbook.createWorkbook(new File(destination));

2 Answers 2

2

That will happen if some process is still keeping the file open. E.g. you've created a FileInputStream on the file to read it, but are never calling close() on it after reading.


Unrelated to the problem, the expanded WAR folder is not the best place to use as a permanent storage. All those files in the expanded WAR folder will get lost whenever you redeploy the WAR. Also hardcoding a servletcontainer-specific path in the code makes it totally unportable.

If your actual intent is to return the Excel file on a per-request basis to the client using a servlet, then you should be using

WritableWorkbook workBook = Workbook.createWorkbook(response.getOutputStream());
// ...

This way it writes to the response immediately without the need for an intermediate file.

Sign up to request clarification or add additional context in comments.

1 Comment

If something was still keeping the file open then why would if work if a file with the name "file.xls" but not with the name "file1.xls". Wouldn't it fail in both situations?
1

Use the File.createTempFile(prefix, suffix, directory) API:

String localName = new File(fileName).getName();
String nameNoExt = localName.substring(0, fileName.lastIndexOf("."));
String extension = localName.substring(fileName.lastIndexOf(".")); // need to include the .
File directory = new File("c:/apache-tomcat-7.0.8/webapps/reports/");
File destFile = File.createTempFile(nameNoExt, extension, directory)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.