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.
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.
Unhide the file first:
Runtime.getRuntime().exec("attrib -H myFile.txt");
^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With