Rollerball
Rollerball

Reputation: 13108

Clarification about FileWriter

Directly from the related FileWriter API:

Whether or not a file is available or may be created depends upon the underlying platform.

Does it mean that it's not guaranteed that:

//assume all the exceptions have been taken care of.
    File file = new File("/home/user/Desktop/lol.txt");
    FileWriter writer = new FileWriter(file);

creates the file even though the file does not exist? I thought it was guaranteed. Do they mean maybe due to priviledge and things?

Thanks in advance.

Upvotes: 0

Views: 52

Answers (1)

AllTooSir
AllTooSir

Reputation: 49362

It may be unable to create file if you don't have permission to create that file where you're trying to create it.

Look at the documentation:

Throws:

IOException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason

You can use the AccessController#checkPermission(java.security.Permission) method and pass FilePermission object to check what permissions you have before trying to create a file.

Upvotes: 3

Related Questions