tags:

views:

226

answers:

6

Following on from a previous question, for some reason when I use the following code :

    final File tmpDir = new File("C:/TEMP/", zipFile.getName());

    if(!tmpDir.mkdir() && tmpDir.exists()) {
        System.err.println("Cannot create: " + tmpDir);
        System.exit(0);
    }

I get an error (Cannot create: C:\TEMP\aZipFile) however, if I use the following:

    final File tmpDir = new File(System.getProperty("java.io.tmpdir"), zipFile.getName());

    if(!tmpDir.mkdir() && tmpDir.exists()) {
        System.err.println("Cannot create: " + tmpDir);
        System.exit(0);
    }

it works perfectly. My problem is that I want to use C:\TEMP as this is consistent with the rest of the project I am working on.

Again, I'm using Java 1.4 on Windows XP and JDeveloper IDE.

A: 

Is it because you don't have write access to "C:/TEMP/" or that TEMP folder doesn't exist?

Ngu Soon Hui
+1  A: 

Well, if System.getProperty("java.io.tmpdir") does not return 'C:\TEMP' it's not the same. While I'd suggest to rely on java.io.tmpdir, you could also make sure that C:\TEMP exists - or create it if needed: ``;

File temp = new File("C:/TEMP/");
if (!temp.exists()) temp.mkdir();
File tmpDir = new File(temp, zipFile.getName());

Alternatively, you could change your code to

final File tmpDir = new File(System.getProperty("java.io.tmpdir"), zipFile.getName());

// note the change from mkdir to mkdirs
if(!tmpDir.mkdirs() && !tmpDir.exists()) {
    System.err.println("Cannot create: " + tmpDir);
    System.exit(0);
}

EDIT: I've just seen the answer by atomice and he's right: it's !tmpDir.exists() rather than tmpDir.exists()

sfussenegger
+3  A: 
if(!tmpDir.mkdir() && tmpDir.exists())

Shouldn't this be:

if(!tmpDir.mkdir() && !tmpDir.exists())
atomice
Why the upvote? Does this answer the question?
Ngu Soon Hui
Yes, first part of the sentence tries to create the temp dir, returning true if it creates or false if it can't create or already exists, this posibility is covered by the second part of the if. So that the if is true only if the temp dir doesn't exist and it can't be created
Telcontar
A: 

Did you compare the result of 'System.getProperty("java.io.tmpdir")' with what you are trying? Also, on WindowsXP I'd go for "C:\Temp\" as the directory name.

jhwist
A: 

Is there a file in the temp dir with the name you want that is locked?

Martin Hohenberg
+1  A: 

Why don't use File.createTempFile, isn't it what you trying to archive?

Nikolay Ivanov