@Test public void testGetDiskPartitionSpaceUsedPercent() { String tmpDir = System.getProperty("java.io.tmpdir"); assertThat(UtilAll.getDiskPartitionSpaceUsedPercent(null)).isCloseTo(-1, within(0.000001)); assertThat(UtilAll.getDiskPartitionSpaceUsedPercent("")).isCloseTo(-1, within(0.000001)); assertThat(UtilAll.getDiskPartitionSpaceUsedPercent("nonExistingPath")).isCloseTo(-1, within(0.000001)); assertThat(UtilAll.getDiskPartitionSpaceUsedPercent(tmpDir)).isNotCloseTo(-1, within(0.000001)); }
public static double getDiskPartitionSpaceUsedPercent(final String path) { if (null == path || path.isEmpty()) { return -1; } try { File file = new File(path); if (!file.exists()){ return -1; } long totalSpace = file.getTotalSpace(); if (totalSpace > 0) { long freeSpace = file.getFreeSpace(); long usedSpace = totalSpace - freeSpace; return usedSpace / (double) totalSpace; } } catch (Exception e) { return -1; } return -1; }
UtilAll { public static double getDiskPartitionSpaceUsedPercent(final String path) { if (null == path || path.isEmpty()) { return -1; } try { File file = new File(path); if (!file.exists()){ return -1; } long totalSpace = file.getTotalSpace(); if (totalSpace > 0) { long freeSpace = file.getFreeSpace(); long usedSpace = totalSpace - freeSpace; return usedSpace / (double) totalSpace; } } catch (Exception e) { return -1; } return -1; } }
UtilAll { public static double getDiskPartitionSpaceUsedPercent(final String path) { if (null == path || path.isEmpty()) { return -1; } try { File file = new File(path); if (!file.exists()){ return -1; } long totalSpace = file.getTotalSpace(); if (totalSpace > 0) { long freeSpace = file.getFreeSpace(); long usedSpace = totalSpace - freeSpace; return usedSpace / (double) totalSpace; } } catch (Exception e) { return -1; } return -1; }  }
UtilAll { public static double getDiskPartitionSpaceUsedPercent(final String path) { if (null == path || path.isEmpty()) { return -1; } try { File file = new File(path); if (!file.exists()){ return -1; } long totalSpace = file.getTotalSpace(); if (totalSpace > 0) { long freeSpace = file.getFreeSpace(); long usedSpace = totalSpace - freeSpace; return usedSpace / (double) totalSpace; } } catch (Exception e) { return -1; } return -1; }  static int getPid(); static String currentStackTrace(); static String offset2FileName(final long offset); static long computeEclipseTimeMilliseconds(final long beginTime); static boolean isItTimeToDo(final String when); static String timeMillisToHumanString(); static String timeMillisToHumanString(final long t); static long computNextMorningTimeMillis(); static long computNextMinutesTimeMillis(); static long computNextHourTimeMillis(); static long computNextHalfHourTimeMillis(); static String timeMillisToHumanString2(final long t); static String timeMillisToHumanString3(final long t); static double getDiskPartitionSpaceUsedPercent(final String path); static int crc32(byte[] array); static int crc32(byte[] array, int offset, int length); static String bytes2string(byte[] src); static byte[] string2bytes(String hexString); static byte[] uncompress(final byte[] src); static byte[] compress(final byte[] src, final int level); static int asInt(String str, int defaultValue); static long asLong(String str, long defaultValue); static String formatDate(Date date, String pattern); static Date parseDate(String date, String pattern); static String responseCode2String(final int code); static String frontStringAtLeast(final String str, final int size); static boolean isBlank(String str); static String jstack(); static String jstack(Map<Thread, StackTraceElement[]> map); static boolean isInternalIP(byte[] ip); static String ipToIPv4Str(byte[] ip); static byte[] getIP(); static void deleteFile(File file); }
UtilAll { public static double getDiskPartitionSpaceUsedPercent(final String path) { if (null == path || path.isEmpty()) { return -1; } try { File file = new File(path); if (!file.exists()){ return -1; } long totalSpace = file.getTotalSpace(); if (totalSpace > 0) { long freeSpace = file.getFreeSpace(); long usedSpace = totalSpace - freeSpace; return usedSpace / (double) totalSpace; } } catch (Exception e) { return -1; } return -1; }  static int getPid(); static String currentStackTrace(); static String offset2FileName(final long offset); static long computeEclipseTimeMilliseconds(final long beginTime); static boolean isItTimeToDo(final String when); static String timeMillisToHumanString(); static String timeMillisToHumanString(final long t); static long computNextMorningTimeMillis(); static long computNextMinutesTimeMillis(); static long computNextHourTimeMillis(); static long computNextHalfHourTimeMillis(); static String timeMillisToHumanString2(final long t); static String timeMillisToHumanString3(final long t); static double getDiskPartitionSpaceUsedPercent(final String path); static int crc32(byte[] array); static int crc32(byte[] array, int offset, int length); static String bytes2string(byte[] src); static byte[] string2bytes(String hexString); static byte[] uncompress(final byte[] src); static byte[] compress(final byte[] src, final int level); static int asInt(String str, int defaultValue); static long asLong(String str, long defaultValue); static String formatDate(Date date, String pattern); static Date parseDate(String date, String pattern); static String responseCode2String(final int code); static String frontStringAtLeast(final String str, final int size); static boolean isBlank(String str); static String jstack(); static String jstack(Map<Thread, StackTraceElement[]> map); static boolean isInternalIP(byte[] ip); static String ipToIPv4Str(byte[] ip); static byte[] getIP(); static void deleteFile(File file); static final String YYYY_MM_DD_HH_MM_SS; static final String YYYY_MM_DD_HH_MM_SS_SSS; static final String YYYYMMDDHHMMSS; }