Results 1 to 4 of 4
  1. #1
    KoalaInRehab is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default AccessDeniedException weirdness

    Hi guys,

    I am getting some really weird behavior with File IO. In my code, I wrote up a custom Logger class that basically logs some strings into appropriate log files. I am working in the Eclipse IDE, so my working directory should be the top level project directory. Here is the structure of the folders:

    WorkingDir/logs/log.txt
    WorkingDir/src/logDir/Logger.java
    WorkingDir/src/mainCodeDir/LoggerUser.java
    WorkingDir/bin/logDir/Logger.class
    WorkingDir/bin/mainCodeDir/LoggerUser.class

    The logs folder was created in Eclipse (File -> New -> Folder). So here's the weird part, when I test the Logger code by creating a new logger and calling the logging method in a static method (like the main method of Logger or LoggerUser) it works just fine, and creates the log.txt (after deleting the old one). However, when I do the same exact thing from a non-static method in LoggerUser for example, an AccessDeniedException is thrown on the log.txt:

    Caused by: java.nio.file.AccessDeniedException: logs\log.txt
    at sun.nio.fs.WindowsException.translateToIOException (Unknown Source)
    at sun.nio.fs.WindowsException.rethrowAsIOException(U nknown Source)
    at sun.nio.fs.WindowsException.rethrowAsIOException(U nknown Source)
    at sun.nio.fs.WindowsFileSystemProvider.newByteChanne l(Unknown Source)
    at java.nio.file.spi.FileSystemProvider.newOutputStre am(Unknown Source)
    at java.nio.file.Files.newOutputStream(Unknown Source)
    at java.nio.file.Files.newBufferedWriter(Unknown Source)
    at common.Logger.<init>(Logger.java:24)
    ... 4 more

    This is my simple logging code from Logger.java's constructor (which throws the exception):

    public Logger() {
    try {
    this.logFile = Paths.get("logs\\log.txt");
    Files.deleteIfExists(logFile);
    this.logWriter = Files.newBufferedWriter(logFile, Charset.defaultCharset(),
    StandardOpenOption.CREATE, StandardOpenOption.APPEND, StandardOpenOption.WRITE);
    }
    catch(IOException e) {
    throw new RuntimeException("Could not initialize logger: ", e);
    }
    }

    So why would the static/non-static nature of the calling code affect the access rights of the file? Any help would be greatly appreciated.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: AccessDeniedException weirdness

    Does the non-static version have multiple instances of the Logger (eg, it is trying to open up and append to the same file at the same time)?

  3. #3
    KoalaInRehab is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: AccessDeniedException weirdness

    Quote Originally Posted by doWhile View Post
    Does the non-static version have multiple instances of the Logger (eg, it is trying to open up and append to the same file at the same time)?
    No, the logger is a member of the LoggerUser class, so it's created once in the LoggerUser constructor.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

    Default Re: AccessDeniedException weirdness

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. TreeSet weirdness
    By Bulska in forum New To Java
    Replies: 3
    Last Post: 03-18-2011, 03:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •