Skip to content

Instantly share code, notes, and snippets.

@lburgazzoli
Last active January 12, 2016 07:55
Show Gist options
  • Save lburgazzoli/5b4944eaa01bc6ed6074 to your computer and use it in GitHub Desktop.
Save lburgazzoli/5b4944eaa01bc6ed6074 to your computer and use it in GitHub Desktop.
FileSystemUtils.java
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class FileSystemUtils {
private static Object FS;
private static Method GET_ATTRIBUTES;
static {
try {
Field field = File.class.getDeclaredField("fs");
field.setAccessible(true);
FS = field.get(null);
if(FS != null) {
try {
GET_ATTRIBUTES = FS.getClass().getDeclaredMethod("getBooleanAttributes0", File.class);
GET_ATTRIBUTES.setAccessible(true);
} catch (Exception ex) {
GET_ATTRIBUTES = null;
}
}
} catch (Exception e) {
throw new AssertionError(e);
}
}
public static boolean exists(File path) {
try {
return GET_ATTRIBUTES != null
? ((Integer) GET_ATTRIBUTES.invoke(FS, path)) > 0
: path.exists();
} catch (Exception e) {
return path.exists();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment