Changing a hidden file in Java

I have a file that the user loads, and then I run a command in java to hide the file:

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

Now I need to access this hidden file, but I get

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

This works if the file is not hidden, but the file must be hidden so that the user does not modify it. So how can I modify a hidden file? Is there any way to do this in Java?

Thanks for your ideas.

+3
source share
2 answers

Dolph, . -, "attrib" (Windows). -, , , , ( ). . , Windows " ". , System "user.home":

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

, :

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

* nix .xyz :

//*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");

os.name, , , .

+5

:

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

Source: https://habr.com/ru/post/1746412/


All Articles