Last active
January 12, 2016 07:55
-
-
Save lburgazzoli/5b4944eaa01bc6ed6074 to your computer and use it in GitHub Desktop.
FileSystemUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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