This program will help you modify the permission of a file.on Unix operating system. You need to pass the absolute path of the file and the permission(e.g. 775,700,777 etc.) you wnat to give, as the command line parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | import java.io.*; import java.util.*; /** ** The FilePerm class has a method updateFilePerm ** to assist in changing the permission of a file on Unix OS, ** through java program **/ public class FilePerm { /** updateFilePerm method coverts permission of file * by using chmod command which allow either of -rwx- * permission to the file. * */ public static boolean updateFilePerm(String fileName, int mode) throws Exception { String method = "updateFilePerm" ; boolean updateError = false ; /** retry concept */ int retry = 0 ; /** changeMod */ String changemod = null ; /** Runtime class object */ Runtime rt = null ; /** Process class object */ Process proc = null ; if ( null == fileName || ( "" ).equals(fileName)) { throw new Exception( "Source File is null:Null pointer Exception" ); } if (mode < 0 || mode > 777 ) { throw new Exception( "Invalid Mode" ); } try { rt= Runtime.getRuntime(); changemod = "chmod" + " " + mode + " " + fileName; do { /** Changing the mode of the file */ try { proc = rt.exec(changemod); proc.waitFor(); } catch (IOException ioex) { throw ioex; } /** In case of abnormal termination of proecss */ if (proc.exitValue() != 0 ){ updateError= true ; retry++; } else { updateError = false ; } } while (updateError && retry < 3 ); return updateError; } catch (Exception ex){ throw ex; } } public static void main(String[] args) { try { updateFilePerm(args[ 0 ],Integer.parseInt(args[ 1 ])); } catch (Exception Ex) { System.out.println( "Exception in giving permission to the file :" +Ex); } } } |