FileUtil { public static String expandUserHomeInPath(String path) { if (path.matches("^~(/|$).*")) { return path.replaceFirst("~", System.getProperty("user.home")); } else { return path; } }  static void saveInputStreamToFile(InputStream inputStream, String filePath); static String getStringFromFile(String fileName); static String expandUserHomeInPath(String path);  }
@Test public void testExpandUserHomeInPath() { String path = "~/test_dir/file"; Assert.assertEquals(System.getProperty("user.home") + "/test_dir/file", FileUtil.expandUserHomeInPath(path)); path = "~"; Assert.assertEquals(System.getProperty("user.home"), FileUtil.expandUserHomeInPath(path)); path = "~user"; Assert.assertEquals(path, FileUtil.expandUserHomeInPath(path)); path = "/test/test_dir"; Assert.assertEquals(path, FileUtil.expandUserHomeInPath(path)); }