Created
November 19, 2024 01:55
-
-
Save hajubal/dcdac0e58803f6db7a4caee5429bba71 to your computer and use it in GitHub Desktop.
DiskUsageMonitor
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.text.DecimalFormat; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class DiskUsageMonitor { | |
public List<DiskUsage> getDiskUsage() { | |
List<DiskUsage> diskUsages = new ArrayList<>(); | |
// 모든 파일시스템 root 가져오기 | |
File[] roots = File.listRoots(); | |
for (File root : roots) { | |
DiskUsage usage = new DiskUsage(); | |
usage.setPath(root.getAbsolutePath()); | |
usage.setTotalSpace(formatSize(root.getTotalSpace())); | |
usage.setUsableSpace(formatSize(root.getUsableSpace())); | |
usage.setUsedSpace(formatSize(root.getTotalSpace() - root.getUsableSpace())); | |
usage.setUsagePercentage(calculateUsagePercentage(root)); | |
diskUsages.add(usage); | |
} | |
return diskUsages; | |
} | |
private String formatSize(long size) { | |
if (size <= 0) return "0"; | |
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" }; | |
int digitGroups = (int) (Math.log10(size) / Math.log10(1024)); | |
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) | |
+ " " + units[digitGroups]; | |
} | |
private double calculateUsagePercentage(File root) { | |
long total = root.getTotalSpace(); | |
long free = root.getUsableSpace(); | |
if (total == 0) return 0.0; | |
return ((double) (total - free) / total) * 100; | |
} | |
} | |
class DiskUsage { | |
private String path; | |
private String totalSpace; | |
private String usableSpace; | |
private String usedSpace; | |
private double usagePercentage; | |
// Getters and Setters | |
public String getPath() { return path; } | |
public void setPath(String path) { this.path = path; } | |
public String getTotalSpace() { return totalSpace; } | |
public void setTotalSpace(String totalSpace) { this.totalSpace = totalSpace; } | |
public String getUsableSpace() { return usableSpace; } | |
public void setUsableSpace(String usableSpace) { this.usableSpace = usableSpace; } | |
public String getUsedSpace() { return usedSpace; } | |
public void setUsedSpace(String usedSpace) { this.usedSpace = usedSpace; } | |
public double getUsagePercentage() { return usagePercentage; } | |
public void setUsagePercentage(double usagePercentage) { | |
this.usagePercentage = Math.round(usagePercentage * 100.0) / 100.0; | |
} | |
@Override | |
public String toString() { | |
return "DiskUsage{" + | |
"path='" + path + '\'' + | |
", totalSpace='" + totalSpace + '\'' + | |
", usableSpace='" + usableSpace + '\'' + | |
", usedSpace='" + usedSpace + '\'' + | |
", usagePercentage=" + usagePercentage + '%' + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment