Skip to content

Instantly share code, notes, and snippets.

@jca02266
Created December 4, 2016 05:27
Show Gist options
  • Save jca02266/53072e177ea0da09140ce2980aa9539d to your computer and use it in GitHub Desktop.
Save jca02266/53072e177ea0da09140ce2980aa9539d to your computer and use it in GitHub Desktop.
package sample;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Pattern;
public class PathUtils {
public static boolean exists(String path) {
File file = new File(path);
return file.exists();
}
public static String path(String... paths) {
StringBuilder sb = new StringBuilder();
for (String path: paths) {
path = path.replace('\\', '/'); // convert to UNIX path
if (sb.length() > 0 && sb.charAt(sb.length()-1) != '/') {
sb.append("/");
}
sb.append(path);
}
return sb.toString();
}
public static String path(File file) {
return path(file.getPath().toString());
}
public static String basename(String path) {
return new File(path).getName();
}
public static String dirname(String path) {
return path(new File(path).getParent());
}
public static void removeFile(String path) {
File file = new File(path);
file.delete();
}
public static void touchFile(String path) throws IOException {
File file = new File(path);
file.createNewFile();
}
public static String getTmpdir() {
return path(System.getProperty("java.io.tmpdir"));
}
public static String getCurrentDirectory() {
return path(System.getProperty("user.dir"));
// return path(new File(".").getAbsoluteFile().getParent());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment