Solved

To change File Permission in JAVA

Posted on 2002-07-01
9
242 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
Comment Utility
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
Comment Utility
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
Comment Utility
can u tell me on which platform you have this requirement.
0
 

Author Comment

by:Prasanna23
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Accepted Solution

by:
ruslanas earned 100 total points
Comment Utility
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
Comment Utility
Thanks ruslanas.

Best Regards,
Prasanna
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 71
endX challenge 2 48
Systems talking to each other 5 107
base64 decode encode 12 96
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now