• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

To change File Permission in JAVA

Hi,

I have a typical scenario. I have a plain class. Once invoked it goes fetches some data, does some processing and returns back. The processing output is written to a Text File.
Every time the class is invoked it has to append the process output to the same Text file. As per clients requirement the Text file should be Read-Only. Is there any way by which when invoked the Program changes permission, appends to the file and makes it again as Read-Only .

Best Regards,
Prasanna
0
Prasanna23
Asked:
Prasanna23
1 Solution
 
CEHJCommented:
You could, in Windows:
invoke attrib +R <filename>
do your stuff
invoke attrib -R <filename>

or in Unix
invoke chmod +r <filename>
do your stuff
invoke chmod -r <filename>
0
 
ucero007Commented:
You could use the File class, it has a method named setReadOnly()

File file = new File("textFile.txt");

// Append your data

file.setReadOnly();

ucero007
0
 
viju_chackoCommented:
can u tell me on which platform you have this requirement.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Prasanna23Author Commented:
Dear all,

I want to do appending from Java class(Not in command line or not through JNI). Since the application is Java, Target Environment requirement is both Windows and Solaris.

"File" setReadOnly() method makes the file Readonly first time. I tried it before itself but subsequent time it will through Security Exception if we try to append to same File.

Is there any way during subsequent times we change the file permission, append the contents to same file and set it as Read Only again.

Best Regards,
Prasanna
0
 
ruslanasCommented:
Java prohibits clearing of a read only attribute, it's SUN's policy and they don't want to change it. So you can make file read-only, but you can not make it writable again from Java.

Like some other people commented:
1) you could use your own JNI function;
2) execute some OS specific command (i.e. chmod in *nix).

More info:
http://developer.java.sun.com/developer/bugParade/bugs/4089881.html
0
 
Prasanna23Author Commented:
Thanks ruslanas.

Best Regards,
Prasanna
0
 
CEHJCommented:
>>Since the application is Java, Target Environment requirement is both Windows and Solaris.

This is why using native methods is not a good solution. It would be better to detect the OS and do it via the shell as mentioned. The same code could then be use for each OS.
0
 
Prasanna23Author Commented:
Dear CEHJ,

When u mentioned "attrib" and "chmod", unfortunately I didnt get the meaning as i also know them as commands and i have used them 100 times in Solaris (chmod).

I was thinking JNI as the option. Today I tried it with "Runtime" class, "exec()" method and I could do it. I dont know whether its a correct way to do. I am handling the Security, IO Exception anyway.

Runtime r = Runtime.getRunTime();
r.exec("attrib -R sample.txt");  // For Windows
or
r.exec("chmod 777 sample.txt");   // For Solaris

Any suggestions on the above approach,

Best Regards,
Prasanna
0
 
CEHJCommented:
Well it looks as if my points have gone - ruslanas's posting wasn't even there when I sent mine in :-)

>>unfortunately I didnt get the meaning as i also know them as commands

Well they *are* commands. I think maybe I didn't make myself absolutely clear. I meant for these commands to be executed by the Runtime class. First, though, you'd have to do something like:

public class OsDetect {

  public static void main(String[] args){

    String os = System.getProperty("os.name").toUpperCase();
    if(os.indexOf("WINDOWS") <= 0){
      // You're windozing
    }
    else {
      // You could be dealing with something *serious*!
    }
  }
}


0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now