• Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing File Permission

 
Sathya Prabhu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am trying out a java program which changes the file access rights of a file from read to write. But when I execute the program it is not working neither with Windows nor Unix.Can anyone of you help on this.I have given the sample program I tried for reference :

import java.io.FilePermission;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter
import java.io.FileWriter;
import java.io.PrintWriter;


public class sample {
public static void main(String args[]) throws IOException,NullPointerException,Exception {

FileReader fr = new FileReader("File1.csv");
BufferedReader br = new BufferedReader(fr);
FilePermission permission;
permission= new java.io.FilePermission("File2.csv", "write");
FileWriter fw = new FileWriter("File2.csv");
PrintWriter pw = new PrintWriter(fw);

String headerName = br.readLine().trim();
pw.println(headerName);
fw.flush();
fw.close();
pw.close();
}
}

Thanks in advance!!!
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FilePermission has nothing to do with OS-level access permissions to a file - it controls and enforces access to files from within Java code.

Java has no built-in facilities for doing this (the other way around may be possible using File.setReadOnly). You will have to use OS-level methods, e.g. on Unix something like Runtime.exec("chmod o+w file1.csv"). (I'm typing off the top of my head, so I may have the particulars wrong.)
[ May 25, 2006: Message edited by: Ulf Dittmer ]
 
Sathya Prabhu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I did try with
Process run= Runtime.getRuntime().exec("chmod o+w File2.csv");

But it is throwing an IOException CreateProcess Error=2.

When searched for the reason I found that error=2 indicates 'the file not found' found.

How can I tackle this?Please help!!!
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using a full absolute path instead of just the filename.
 
Sathya Prabhu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have tried tried giving absolute path also.But it doesnt work!!!
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried the code and it worked perfectly fine:



It should work as long as the file is directly located in the current directory. Otherwise I would have had to specified the full path. In my case it would have been;



Another posibility is that you do not have permission to alter the file properties. Are you the owner of the file? Do you have access to the file path?

For instance, this command produces an IOException in my computer:

Runtime.getRuntime().exec("chrgp root jedi.bin");

Just because I do not have enough priviles to execute it.
 
dema rogatkin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can check a user name your program acts from then do ls -l to figure out ownership of file. If you found mismatch, then your program can ask for additiona privileges and execute su first. Actually I'd recommend to write a script with the same name as for Windows as for Unix. This script should take care of all cases which are OS dependent.
 
Kamal Ahmed
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this method
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!