tags:

views:

107

answers:

2

I have a file that user downloads and then I execute a command within java to hide the file:

Runtime.getRuntime().exec("attrib +H myFile.txt");

Now later I need to access that hidden file but I'm getting

java.io.FileNotFoundException: myFile.txt (Access is denied)

This works if file is not hidden, but the file needs to be hidden so user wont modify it. So how am I supposed to modify the hidden file? Is there any way to do this in Java?

Thanks for your ideas.

+2  A: 

Unhide the file first:

Runtime.getRuntime().exec("attrib -H myFile.txt");
                                  ^
Dolph
+2  A: 

I agree with Dolph, however you might also consider alternatives to using hidden files. The first is you are now dependent on the (Windows) "attrib" command. Secondly, just because a file is marked as hidden does not mean the user can not see or modify it (I have my machine set to always display hidden files). As an alternative you might consider using standard directory locations and filenaming conventions. For example in Windows a standard location to put your application data is in the folder "Application Data". You can find this folder using the System property "user.home":

System.out.println(System.getProperty("user.home"));
//prints out something like C:\Documents And Settings\smithj

You can use this create your own Application Data folder:

//For Windows
File appDataDir = new File(System.getProperty("user.home"), "Application Data\\MyWidgetData");

Similarly in *nix environments applications usually keep their data in a .xyz directory in the home directory:

//*nix OSes
System.out.println(System.getProperty("user.home"));
//prints out something like /user/home/smithj
File appDataDir = new File(System.getProperty("user.home"), ".MyWidgetData");

You can look at the property os.name to determine what environment you are running on and construct a correct path based on that.

M. Jessup
Alright thanks for your responses.
Marquinio