When we are transfering files from one platform to another for eg: windows to unix or vice versa , in that case there will be default mode assigned to the transfered file . so to change the mode of the transfered file we can use the "Change in accessibility of file" code snippet in our logic.
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class updateFilePerm {
public static void main(String[] args) {
boolean updateError = false;
int retry = 0;
String changemod = null;
Runtime rt = null;
Process proc = null;
Scanner in = new Scanner(System.in);
System.out.println("Enter the FileName");
String fileName = in.nextLine();
File file = new File(fileName);
boolean b = false;
b = file.exists();
if (!b) {
System.out.println("File doesnot Exist");
System.exit(1);
} else {
System.out.println("Enter Mode");
int mode = in.nextInt();
if (null == fileName || ("").equals(fileName)) {
System.out.println("FielName cannot be null ");
System.exit(1);
}
if (mode < 0 || mode > 777) {
System.out.println("Invalid Mode entered");
System.exit(1);
}
try {
rt = Runtime.getRuntime();
changemod = "chmod" + " " + mode + " " + fileName;
do {
/** Changing the mode of the file */
try {
proc = rt.exec(changemod);
proc.waitFor();
} catch (IOException se) {
System.out.println(se.toString());
}
/** In case of abnormal termination of process */
if (proc.exitValue() != 0) {
updateError = true;
retry++;
} else {
updateError = false;
}
} while (updateError && retry < 3);
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
}