Solved

To change File Permission in JAVA

Posted on 2002-07-01
9
254 Views
Last Modified: 2010-03-31
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
Comment
Question by:Prasanna23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7121550
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
 

Expert Comment

by:ucero007
ID: 7121730
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
 

Expert Comment

by:viju_chacko
ID: 7123669
can u tell me on which platform you have this requirement.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:Prasanna23
ID: 7123706
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
 

Accepted Solution

by:
ruslanas earned 100 total points
ID: 7124263
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
 

Author Comment

by:Prasanna23
ID: 7125843
Thanks ruslanas.

Best Regards,
Prasanna
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7128746
>>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
 

Author Comment

by:Prasanna23
ID: 7129167
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 7129499
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question