code: public static String expand(String filename) {
      if (filename.startsWith("~" + File.separator)) {
        try {
          String home = System.getProperty("user.home");
          if (home != null) {
            return home + filename.substring(1);
          }
        } catch (SecurityException e) {
          // ignore
        }
      }
      return filename;
    }
docs: Expands "~" to the home directory.
  
  @param filename File name
  @return Expanded file name