How to provide file permissions using Java?

Prior to Java 1.6, the only possible way (but not advisable always) is using the Run time class in Java api.

try
{
    Runtime.getRuntime().exec("chmod 777 *.txt");
}
catch (IOException e1)
{
    e1.printStackTrace();
}    

With Java 1.6, you can do the same programmatically. Consider the following code snippet as example.

File f1 = new File("");
if(!f1.exists())
{    
    f1.createNewFile();
    f1.setReadable(true);
    f1.setWritable(true);
    f1.setExecutable(true);
}

Search