Java: Get special folders

/**
 * Returns the temp directory of the operating system
 * with ending /
 * @return
 */
public static String getTempDirectory() {
 String s = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$

 //MAC OS and linux behaviour
 if (s.startsWith("/var/folders/")) s = "/tmp/"; //$NON-NLS-1$ //$NON-NLS-2$

 s = addSeparator(s);

 return s;
}

/**
 * Gets the user directory with ending /
 * @return
 */
public static String getUserDirectory() {
 String s = System.getProperty("user.home"); //$NON-NLS-1$
 s = addSeparator(s);
 return s;
}

/**
 * Returns the application data path, path is returned with ending /
 * @return
 */
public static String getAppData() {
 String path = ""; //$NON-NLS-1$
  String OS = System.getProperty("os.name").toUpperCase(); //$NON-NLS-1$
  if (OS.contains("WIN")) //$NON-NLS-1$
   path = System.getenv("APPDATA"); //$NON-NLS-1$
  else if (OS.contains("MAC")) //$NON-NLS-1$
   path = System.getProperty("user.home") + "/Library/"; //$NON-NLS-1$ //$NON-NLS-2$
  else if (OS.contains("NUX")) //$NON-NLS-1$
   path = System.getProperty("user.home"); //$NON-NLS-1$
  else path =  System.getProperty("user.dir"); //$NON-NLS-1$

  path = addSeparator(path);

  return path;
}

private static String addSeparator(String s) {
 if (!s.endsWith(File.separator)) s += File.separator;
 return s;
}

Werbung



comments powered by Disqus

Copyright Dunkel & Iwer GbR | Datenschutzerklärung | Impressum