Rude-Student..
Rude-Student..

Reputation: 69

Creating new directories

I'm stuck with the fact that no folder is made.

private static File createNewTempDir() {
File baseDir = new File(System.getProperty("java.io.tmpdir"));
String baseNamePrefix = System.currentTimeMillis() + "_" + Math.random() + "-";
LOG.info(System.getProperty("java.io.tmpdir"));
File tempDir = new File(baseDir, baseNamePrefix + "0");
LOG.info(tempDir.getAbsolutePath());

tempDir.mkdirs();

if (tempDir.exists()) {
  LOG.info("I would be happy!");
}
else {
  LOG.info("No folder there");
}
return tempDir;
}

Is there any wrong with it? I can get the LOG that no folders are there...

Upvotes: 0

Views: 96

Answers (1)

Marcelo
Marcelo

Reputation: 4608

Your code is fine, but your conditional is wrong:

if (tempDir.exists()) {
  LOG.info("I would be happy!");
}
else {
  LOG.info("No folder there");
}

The folder is created indeed, you can check that by getting the path and opening on Explorer.

EDIT: It works on Windows at least. I cleaned it up a bit:

   public static void main() {
        File baseDir = new File(System.getProperty("java.io.tmpdir"));
        File tempDir = new File(baseDir, "test0");
        System.err.println(tempDir.getAbsolutePath());

        tempDir.mkdir();

        System.err.println("is it a dir? " + tempDir.isDirectory());
        System.err.println("does it exist? " + tempDir.exists());
    }

Output:

C:\Users\marsch1\AppData\Local\Temp\test0 is it a dir? true does it exist? true

Upvotes: 2

Related Questions