1

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.

1
  • where is your main method? I mean your entry point where you are calling readData() and 'writeData()` Commented Mar 22, 2015 at 13:27

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

I added information to post as P.S., pls look at this.
@Aero Updated my answer if this helps get things going further.
File#canWrite is notoriously bad in detecting any OS implied write restriction. It is not worth using it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.