0

I am creating a directory ("FileAdmin") with the following code:

public class FileAdmin {

    private File dir;

    public FileAdmin() throws IOException{
        this.dir = new File("FileAdmin");
        if(!dir.exists() & !dir.mkdir()) throw new IOException();
}

Main does the following:

public static void main(String[] args) {
    try{
        FileAdmin fa = new FileAdmin();
    } catch (Exception e){
        e.printStackTrace();
    }
}

This is not a problem; when I create a file inside the directory (which succeeds) and try to delete it, I can't. The problem is that mkdir() creates a read-only directory, no matter what I do:

FileAdmin properties on Windows 10 / "Solo lectura" = read-only

Solo lectura -> read-only

I have already tried dir.setWritable(true); but it always returns false. Why is this?

EDIT 1: If I create the dir, uncheck the read-only option in the folder's properties, once i run the code file is deleted

EDIT 2: I'm using Windows 10

  • 2
    Why are you using a bitwise or here `!dir.exists() & !dir.mkdir()`? This will always attempt to make the directory, even if it already exists. Use the logical or `&&` which will shortcut and not evaluate the second half of the expression if the first half is already true. – Michael Jun 27 '17 at 14:37
  • 2
    Aside: don't do work like that in a constructor. Constructors should only initialize the object; add a separate method to create the directory. – Andy Turner Jun 27 '17 at 14:37
  • create the folder manually, in the same location, and check his properties. I assume it would be the same. – c0der Jun 27 '17 at 14:40
  • @AndyTurner I am doing this because the object is a directory, suited for a specific project. Its purpose is on the methods that I'm not showing because they're irrelevant to the problem – Lautaro Paskevicius Jun 27 '17 at 14:40
  • 1
    BTW the black square in read only doesn't mean it is read-only , it is in an unknown state. You can click on that box and change it to ticked or non-ticked state. – SomeDude Jun 27 '17 at 14:46
  • Also it will be helpful if you can post the stacktrace or error when you say : `when I create a file inside the directory (which succeeds) and try to delete it, I can't. ` – SomeDude Jun 27 '17 at 14:54
  • @svasa it doesn't throw an exception, it simply returns `false` – Lautaro Paskevicius Jun 27 '17 at 15:25
  • If this is windows 10, are you trying to create the folder in a protected location (e.g. Program Files), by default those locations are read only. – Goibniu Jun 27 '17 at 15:27
  • @Micheal - those are not a "bitwise" operators. Java "bitwise" operators are defined for integral types, but not for `boolean`. In that context, `&` is a non-short-circuit logical operator. (Lets get the terminology right when we instruct people ... ) – Stephen C Jun 27 '17 at 15:32
  • @Goibniu yes, I am using Windows 10. What's the solution to this? Should I create the dir in a specific location outside my project? – Lautaro Paskevicius Jun 27 '17 at 15:40
  • Windows doesn't have read-only directories, only read-only files. That checkbox is for setting/unsetting the read-only flag on all the files in the folder. Also, `File.delete` will delete read-only files anyway, so that isn't the problem. Are you sure you have the right file path? Try doing a `File.exists` check before the delete instruction. – Sean Van Gorder Jun 27 '17 at 15:44
  • Yes, I am using the exact same path for creation and deletion. – Lautaro Paskevicius Jun 27 '17 at 15:47
  • Try calling `java.nio.file.Files.delete(yourFile.toPath())` and see what exception it throws, it could be more informative. – Sean Van Gorder Jun 27 '17 at 15:59

1 Answers1

0

I don't know if i understand your problem correctly or not but i tried the following code, where a folder FileAdmin is created and inside the folder a file named WritableFile.txt is created and lastly the file has been deleted.

public class Main {
    public static void main(String[] args) throws Exception {
        File directory = new File("FileAdmin");
        directory.mkdir();
        File file = new File("FileAdmin/WritableFile.txt");
        file.createNewFile();

        file.delete();
    }
}

Please correct me if i understood your question differently. I am on windows 10. If you will comment the file.delete() then you will see the file inside the folder.

lambad
  • 1,046
  • 10
  • 21