Code Junkie
Code Junkie

Reputation: 7788

Writing to temp dir Caused by: java.io.FileNotFoundException

Hi I have a java app that I'm trying to write to a tempDir with, but I continue to get the following exception. I'll admit I don't know much about writing files, so hopefully it's something minor I'm missing.

Caused by: java.io.FileNotFoundException

I have the following code.

https://code.google.com/p/sitemapgen4j/

File file = File.createTempFile(System.getProperty("java.io.tmpdir"), null);

        WebSitemapGenerator wsg = new WebSitemapGenerator("http://www.example.com", file);
        wsg.addUrl("http://www.example.com/index.html"); // repeat multiple times
        wsg.write();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputStream outputStream = new FileOutputStream(file);
        out.writeTo(outputStream);

Stack trace

/var/folders/tp/jr6gqrhn7bs18szx5ck_b3qc0000gn/T/
ioc.Registry Problem writing sitemap file /var/folders/tp/jr6gqrhn7bs18szx5ck_b3qc0000gn/T/T2742577239986554001.tmp/sitemap.xml
ioc.Registry Operations trace:
ioc.Registry [ 1] Handling traditional 'action' component event request for account/admin/Index:sitemap.
ioc.Registry [ 2] Triggering event 'action' on account/admin/Index:sitemap
TapestryModule.RequestExceptionHandler Processing of request failed with uncaught exception: org.apache.tapestry5.ioc.internal.OperationException: Problem writing sitemap file /var/folders/tp/jr6gqrhn7bs18szx5ck_b3qc0000gn/T/T2742577239986554001.tmp/sitemap.xml [at classpath:com/domain/auto/pages/account/admin/AdminIndex.tml, line 32]
Caused by: java.io.FileNotFoundException: /var/folders/tp/jr6gqrhn7bs18szx5ck_b3qc0000gn/T/T2742577239986554001.tmp/sitemap.xml (Not a directory)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
    at java.io.FileWriter.<init>(FileWriter.java:90)
    at com.redfin.sitemapgenerator.SitemapGenerator.writeSiteMap(SitemapGenerator.java:198)
    ... 105 more

Upvotes: 1

Views: 2779

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201497

Don't pass null and don't specify the parent directory; because the call to File.createTempFile(String prefix, String suffix) creates an empty file in the default temporary-file directory

try {
    File file = File.createTempFile("something", ".tmp");
    System.out.println(file.getCanonicalPath());
} catch (Exception e) {
    e.printStackTrace();
}

Output (on my machine, changes on every invocation) -

/tmp/something4693207146188341491.tmp

Upvotes: 2

Related Questions