sashaaero
sashaaero

Reputation: 2888

Java read/write access exception FileNotFoundException (Access is denied)

Here I have 2 methods.

private boolean readData(){
    if (!new File(config).exists()) {
        return false;
    } else {
        Hashtable<String, String> data;
        FileInputStream fis;
        try {
            fis = new FileInputStream(config);
            ObjectInputStream oin = new ObjectInputStream(fis);
            data = (Hashtable<String, String>) oin.readObject();
            username = data.get("username");
            password = data.get("password");
            folder = data.get("folder");
            fis.close();
            oin.close();
            return true;
        } catch (FileNotFoundException fnfe) {
            return false;
        } catch (IOException ioe) {
            return false;
        } catch (ClassNotFoundException cnfe) {
            return false;
        }
    }
}

private boolean writeData() {
    try {
        FileOutputStream fos = new FileOutputStream(config);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        Hashtable<String, String> data = new Hashtable<>();
        sout(username);
        data.put("username", username);
        data.put("password", password);
        data.put("folder", folder);
        oos.writeObject(data);
        oos.flush();
        oos.close();
        Runtime.getRuntime().exec("attrib +H " + config);
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

I couldn't find an answer on Stack Overflow.

By the way - exception is always throws from writeData, FileOutputStream.

P.S.
I forgot to add. When the program is started for the first time, it successfully records the file data. As well, it reads data from a file always without problems. Problem is appearing whem i'm trying to rewrite this file.

        if(new File(config).canWrite()){
            sout("Can write");
        } else{
            sout("I can't write");
        }
        writeData();

Is printing I can't write but actually it works. I mean that the file appears and the data is successfully written into it.

Upvotes: 1

Views: 1341

Answers (2)

ha9u63a7
ha9u63a7

Reputation: 6804

It seems that you have issues with your windows/linux file read/right access on the specified file/folder location. You need to ensure that you have sufficient rights to do so before you even run this application. Remember, Java does only as much as you ask of it. Your error message is very clear in directing you to the cause of the problem.

You can check your write access to the folder by doing this.

File f = new File("myfile.txt");
if (f.canWrite)
{ 
     System.out.println("I can write here!!");
}

The other one would be to use SecurityManger and check if you have an exceptions thrown for your write operations:

// Gets the application security manager
SecurityManager sm = System.getSecurityManager();

// Checks for write exceptions
try
{
     sm.checkRead(myFilePath_str); 
     sm.checkWrite(myFilePath_str);

} catch(SecurityException se)

{    // Who threw it?    
     se.printStackTrace(); 
}

Also, I just noticed that you are calling .exec() from Runtime. You can also check for that using checkExec() method - seems like an "Invisible" flag is affecting this operation.

Hope this helps.

Upvotes: 2

eckes
eckes

Reputation: 10423

Your Problem is the hidden flag. I don't know why, but once a file has the hidden flag Java can no longer write to it.

I tested it and you can delete the file (before writing a new one), or if you want to be a bit more transactionally safe you can write to a different file and rename it with REPLACE_EXISTING after you closed it. Both works on top of hidden files.

BTW: I dont think it is a good idea to use Java serialisation to store such a simple information. You are better off with a text format.

Upvotes: 2

Related Questions