Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

To change File Permission in JAVA

Posted on 2002-07-01
9
Medium Priority
?
257 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 400 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

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.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…
Suggested Courses

618 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