user3299745
user3299745

Reputation: 33

File mkdirs() not working as expected

I am currently trying to test if a directory exists and if it does not I want the program to create the full directory specified in reportPath. When I run the code, mkdirs() returns false and the directory specified in reportPath is not created. Doesn't mkdirs() make the entire path if it doesn't exist?

Currently working with the below code:

String reportPath = outputFile+"\\"+providerName+"\\"+testMode+"\\";//+testTarget+"\\";
File dir = new File(reportPath);

if (!dir.exists()) {
    System.out.println("Report Directory does not exist");
    System.out.println("Directory created:" + dir.mkdirs());
}

PrintWriter writer = new PrintWriter(reportPath+fileName, "UTF-8");
int id = 1;
writer.println("ID,Provider,Key Type,Key Size,Operation,Parameters,Data Size,Passed,Error details");

for (TestResult r : results) {
    writer.println(Integer.toString(id) + "," + r.provider + "," + r.keyType + "," + r.keySize + "," + r.operation + "," + r.parameter + "," + r.dataSize + "," + r.passed  + "," + r.errorDetails);
        id++;
}
writer.close();

When I run it, I get the following error:

java.io.FileNotFoundException: <path> (Access is denied)
at java.io.FileOutputStream.open0(Native Method)
(followed by a long list of errors)

Upvotes: 0

Views: 6660

Answers (2)

VGR
VGR

Reputation: 44414

java.io.File is old and obsolete. Use the java.nio.file package instead:

Path dir = Paths.get(outputFile, providerName, testMode);
Files.createDirectories(dir);

PrintWriter writer = new PrintWriter(Files.newBufferedWriter(dir.resolve(fileName)));

Most methods of java.io.File just return a boolean, without telling you what went wrong. Methods of Files always succeed if they return; if they fail, they throw an informative and useful exception.

Upvotes: 1

Stephen C
Stephen C

Reputation: 719709

You say:

permissions seem good

Appearances can be deceptive!

The "Access is denied" is happening because the operating system won't let you either access / create / write to a file or directory. You can assume that it is only doing this because something about your request violates the access control rules.

Here are some of the possibilities.

  1. The path you are using may referring to some other file or directory than what you think; e.g. it may be a relative path and the current directory is not what you think, or there might be a error in the pathname (spelling, case-sensitivity, etc)

  2. The application may not be running as the correct user. Or on Windows, it may not have administrator privilege (if that is required). Or the user many not be in the correct group (on UNIX/Linux ...)

  3. The permissions may not what you think. Have you checked the group permissions? Have you checked for ACLs? Did you check all directories in the path ... starting from the file system root?

  4. On Linux, this could be something to do with SELinux, or AppArmor, or running in a Docker container, or something like that.

  5. If you are trying to create a directory on a locally mounted remote file system, it could be due to a mismatch between local and remote identity system.

  6. If you are trying to use GPFS / AFM caches, NFS identity mapping, etcetera .....


Summary: something is wrong with the permissions / access control, and that is where the problem is.

Upvotes: 2

Related Questions