File Write Protected | sorry if this is the wrong forum
I am writing an address book and when you change the name of a contact, the old file needs to be deleted. I am writing this using eclipse running on an Ubuntu 6.06LTS box. When I try to delete the file using .delete(), it is unable to. Using .canWrite() returns the file as being write protected. I used the chmod command to set the permissions to 777 ( read/write/execute for owner/group/everyone ) but it still sees it as protected. I have double, triple, and quadruple checked that I close all streams that could possibly be accessing the file.
What should I do?
Maybe some other process has the file open?
are you sure your program isn't reading the file and then trying to delete it before closing the reading stream?
The only thing that accesses it would be a check to see if that file exists...
boolean fileExists = (new File("contacts/" + first + ' ' + last + ".con")).exists();
if (fileExists) {
//Do nothing, just overwrite file...
} else { //file alwasy write protected?!
//Delete original contact file, then write new file
boolean write = (new File(addressBookPanel.contactList.getSelectedValue().toString() + ".con")).canWrite();
if (!write) {
System.err.println("File \"" + addressBookPanel.contactList.getSelectedValue().toString() + ".con\" write protected.");
}
boolean deleted = (new File(addressBookPanel.contactList.getSelectedValue().toString() + ".con")).delete();
if (!deleted) {
System.err.println("Deletion of contact file \"" + addressBookPanel.contactList.getSelectedValue().toString() + ".con\" failed.");
}
}
Message was edited by:
mazikowski
Why are you trying to delete a file if the target file doesn't exist? and why are you concluding that a file was write-protected when File.exists() returns false?Something wrong here.
No, what it does is it checks if the file first + " " + last + ".con" exists. If it does, meaning that the name for that contact hasn't been altered, it just saves over that file. If the file doesn't exist, meaning the name has been altered, it deletes the old file(currently selected name in the JList) and creates a new one using the new name.
so, nobody knows what could be causing this or how i could fix it?i have tried everything i can think of
File.canWrite() returns false if the file doesn't exist as well as if it is write protected. Is that a possibility? That would also explain the delete failure.
How did I not think of that!?!?
Well, that's my problem, it is unable to find the file... according to my test output:
The name of the file it should delete, based on the .toString() output from a JList (the same method used to open the file, which works fine)
I find it strange that it can find the file and open it, yet .exists() returns false. Any ideas on what could possibly cause something like this?
Thanks for the help!
James Mazikowski
Typo somewhere. You keep creating new File objects ostensibly representing the same thing. Construct one, and use it to both existence-test and open the file.
I feel really stupid now. The reason it said the file didn't exist was because it was looking for ./file instead of ./contacts/file!Thanks for your help!
> are you sure your program isn't reading the file and then trying > to delete it before closing the reading stream?A sidenote: under Unix including Linux it is possible to delete (unlink) or rename (move) an open file.