skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Sunday, October 14, 2012

Change in accessibilty of File

Posted by Raju Gupta at 1:00 AM – 0 comments
 
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());
   }
  }

 }

}


Labels: Java File Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger