Modify a hidden file in Java

后端 未结 2 870
失恋的感觉
失恋的感觉 2020-12-21 04:35

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\");


        
相关标签:
2条回答
  • 2020-12-21 05:02

    Unhide the file first:

    Runtime.getRuntime().exec("attrib -H myFile.txt");
                                      ^
    
    0 讨论(0)
  • 2020-12-21 05:09

    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.

    0 讨论(0)
提交回复
热议问题