Objective
I want that whenever someone uploads a file (name is Accounts 0998.csv ) in /opt/file/incoming directory. Its permissions gets change to chmod 664 ie rw-rw-r I am using linux.
I want to automate this process so I am writing a java program but its working
package com.reader.file;
import java.io.File;
import java.io.IOException;
public class GrantPermission
{
public static void main( String[] args )
{
try {
File file = new File("/opt/file/Accounts 0998.csv");
if(file.exists()){
System.out.println("File exists.");
//using PosixFilePermission to set file permissions 664
Set<PosixFilePermission> perms = new
HashSet<PosixFilePermission>();
//add owners permission
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
//add group permissions
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.GROUP_WRITE);
//add others permissions
perms.add(PosixFilePermission.OTHERS_READ);
Files.setPosixFilePermissions(file.toPath(), perms);
} catch (IOException e) {
e.printStackTrace();
}
}
else{
System.out.println("File does not exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Additional Information
I am using WSO2 ESB it first search for a file then If file exists then I want to change its permission (via using Class mediator ie JAVA ) and then move it to another directory but my GOAL is change file permissions to rw-rw-r
You can use NIO.2 if you are using java7 or later.
How do i programmatically change file permissions?
//if you need rw-rw-r permissions public void setPermission(File file) throws IOException{ Set<PosixFilePermission> perms = new HashSet<>(); perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_WRITE); perms.add(PosixFilePermission.OTHERS_READ); perms.add(PosixFilePermission.OTHERS_WRITE); perms.add(PosixFilePermission.GROUP_READ); Files.setPosixFilePermissions(file.toPath(), perms); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.