2

I have this code trying to create and write in my Json File, but how can I do it Hidden in any OS (Windows or Mac)

File file = new File(System.getProperty("user.home") + File.separator + "Documents" + File.separator + "targetappConfig.json");

if (!(file.exists())) {

  org.json.simple.JSONArray userDetails = new org.json.simple.JSONArray();
  userDetails.add(userDetail);
  jsonObj.put("users", userDetails);
  FileWriter fileWriter = new FileWriter(file);
  fileWriter.write(jsonObj.toString());
  fileWriter.flush();
  fileWriter.close();
}
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
Victor Laerte
  • 6,446
  • 13
  • 53
  • 102
  • what's your definition for _hidden_? – OscarRyz Feb 07 '13 at 15:08
  • You can't. (Because not all OS support hidden files. (Thanks God!)) – Ingo Feb 07 '13 at 15:08
  • 1
    You do this by creating an appropriate method for every OS family. There is no standard universal notion of "hidden" files. Also, a better way to do what you seem to be trying to do would be to keep your private files in a private folder intended for this sort of thing. (the appropriate `AppData` directory on Windows, `~/Library/Application Support/Your App Name/` on OS X, and `~/.config/yourappname/` on Linux.) – millimoose Feb 07 '13 at 15:20
  • 1
    Is the goal just to store user preferences in a persistent place without the user wondering why a file suddenly appeared? Because then you could just use the [Preferences](http://docs.oracle.com/javase/6/docs/api/java/util/prefs/Preferences.html) class to store the data in a place appropriate to the platform. – Russell Zahniser Feb 07 '13 at 15:25

2 Answers2

2

What I did was to creat a OSValidator and for each OS I encode my file and save it in a Application Dir (windows: appdata, mac: Application Suport). It seemed to be the easiest to do.

public class OSValidator {
    private static String OS = System.getProperty("os.name").toLowerCase();

    public static boolean isWindows(){
        return (OS.indexOf("win")>=0);
    }

    public static boolean isMac(){
        return (OS.indexOf("mac")>=0);
    }

    public static boolean isUnix() {
        return (OS.indexOf("nix") >=0 || OS.indexOf("nux") >=0 || OS.indexOf("aix") >= 0);
    }

    public static boolean isSolaris(){
        return (OS.indexOf("sunos") >=0);
    }
}



if (OSValidator.isWindows()) {
            System.out.println("This is Windows");
            file = new File(System.getenv("APPDATA") + File.separator + "TargetApp" + File.separator +"config.json");

            if (!file.exists()) {
                try {
                    FileUtils.forceMkdir(file.getParentFile());
                } catch (IOException ex) {
                    Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        } else if (OSValidator.isMac()) {
            System.out.println("This is Mac");

            file = new File(System.getProperty("user.home") + File.separator + "Library" + File.separator + "Application Support"
                    + File.separator + "config.json");
        }
Victor Laerte
  • 6,446
  • 13
  • 53
  • 102
  • Why do you use `File.seperator`? As far as I know, java uses the right seperator on its own, when you use "/". Further, the Validator seems to be pretty equal to [this one](http://www.mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/) ;) – MalaKa Feb 07 '13 at 18:18
1

See this question for Windows and other operating systems that actually support the hidden file attribute. There are even multiple ways to do this.

For Unix/Linux, files and folders whose names start with dot are considered hidden (like .ssh, for instance). These are not visible by default. Surely the user can see them in one turns "show hidden files" on for explorer or uses -a for ls. Still, for the reasons like convenience reasons this should be sufficient.

Community
  • 1
  • 1
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93