Solved

To change File Permission in JAVA

Posted on 2002-07-01
9
253 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
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
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!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
going to wrong jsp page 2 49
ejb message driven bean mdb creation steps 2 35
Java array 21 86
Strange router problem - can't access hotmail.com 14 55
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

733 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