How to change image permission mode to 777 using Java code?

前端 未结 5 1556
野趣味
野趣味 2021-01-12 08:13

I want to give permissions mode value \"777\" to image file using Java code. How can I give that using Java? Because I can\'t delete the image with default permission mode \

相关标签:
5条回答
  • 2021-01-12 08:44

    Here goes the code:-

    List<String> chPermList = new LinkedList<String>();
    chPermList.add("/bin/chmod");
    chPermList.add("777");
    chPermList.add(Path_to_image_file);
    
    
        public void changePermission(List chPermList)
        {
            ProcessBuilder pb = null;
            pb = new ProcessBuilder(chPermList);
                    pb.redirectErrorStream(true);
    
                    Process p = pb.start();
    
                    BufferedReader br = new BufferedReader(new InputStreamReader(p
                            .getInputStream()));
    
                    p.waitFor();
    
                    if(p.exitValue()!=0)
                    {
                        strExpMsg = "The permission change failed with exit value" + p.exitValue();
    
                    }
        }
    
    0 讨论(0)
  • 2021-01-12 08:47

    Unfortunately Java gives few grab to the system file properties, because it would break the gold rule of java "write once, run everywhere".

    Indeed when you write that you want to put a 777 access to the file, it obviously means that you're under unix. But what such a request would have meant under Windows for example ?

    So it shouldn't be a surprise that to address specific OS topics, you would have to use specific functions. That's the price to pay when using a language that claims (with strong reasons) being "platform independent"

    The safer way would be to use a JNI library that implements exactly what you want. A simple but dirty and hazardous way would be to using a Runtime.getRuntime().exec("chmod 777 myFile").

    Why hazardous ? In fact, what happens when one call such exec("blahblah") ?

    The unix process running your java program forks; the "duplicated" process then execs a shell (ksh ? csh ? bash ?? it depends on your unix configuration), that tries to understand and run your "blahblah" sentence.

    If your jvm is actually using 500 MB, the forked process would be the same size when it starts...

    Then the shell will start with the same "shell context" than the java process, that is to say the same PATH, aliases and so on. Even "chmod" could have been aliased, so it would be better to call the full path for chmod (/bin/chmod, /usr/bin/chmod... it depends on the unix flavour...).

    During the shell execution, your java program is freezed, and you have no way to unfreeze it if for any reason the shell hangs.

    And finally, it's not even sure the unix owner of the java program has sufficient rights to change such informations on the file (he could be allowed to create the file, but not to change user level rights, for example).

    Maybe you try to handle at the java level a problem that should be addressed at the OS level ?

    0 讨论(0)
  • 2021-01-12 09:03

    You can use the 'exec' method to run an external command to do the chmod.

    Runtime.getRuntime().exec( "chmod 777 myfile" );
    
    0 讨论(0)
  • 2021-01-12 09:04

    You can create a File object associated with the file then change permissions using setExecutable, setReadable, and setWritable. Naturally, these operations will fail if your program doesn't have permission to change access permissions for the file.

    0 讨论(0)
  • 2021-01-12 09:04

    Its a part of the NIO package. You can do it via java.nio.file.attribute.PosixFilePermission.

    This is only in Java 1.7 though, it has been a known issue for a while now.

    0 讨论(0)
提交回复
热议问题