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.
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 | 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()); } } } } |