1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
public class FreeSpaceDisk {
public static void main(String[] args) throws IOException , AddressException, MessagingException {
String hostName = null;
String rslt_totalSpace = null ;
String rslt_freeSpace = null ;
String rslt_Final = "" ;
String rslt_Hostame = "" ;
double stat_Final = 0 ;
String rslt_F = null ;
/**
* Lecture de HostName computer
*/
/* try {
final InetAddress addr = InetAddress.getLocalHost();
hostName = new String(addr.getHostName());
} catch(final Exception e) {
}//end try
*/
try {
final InetAddress addr = InetAddress.getByName("DT-ITI-MON04");
//hostName = new String(addr.getHostAddress());//pour laisser que l'ip
hostName = new String(addr.getHostName()); // pour laisser que le nom
//hostName = new String(addr.getByName("dt-iti"));
} catch(final Exception e) {
}//end try
System.out.println("Hostname: " + hostName);
System.out.println("----------------------");
List <File>files = Arrays.asList(File.listRoots());
for (File f1 : files) {
String description = FileSystemView.getFileSystemView().getSystemTypeDescription(f1);
/**
* Programme Free space
*/
if (f1.exists()) {
if(description.equals("Local Disk")){
/**
* Afficher taille au format xx Ko ou xx Mo ou Go
*/
//capacité de la partition
long totalSpace = f1.getTotalSpace();
long size = (long) ( totalSpace/ (1024)) + 1;
if (size > 1024 ) {
rslt_totalSpace = (size / (1024*1024)) + " Go";
}
else if (size > 1024) {
rslt_totalSpace = (size / (1024)) + " Mo";
}
else {
rslt_totalSpace = size + " ko";
}
//Espace disponible
long freeSpace = f1.getFreeSpace();
long size1 = (long) ( freeSpace/ (1024)) + 1;
if (size1 > 1024 ) {
rslt_freeSpace = (size1 / (1024*1024)) + " Go";
}
else if (size1 > 1024) {
rslt_freeSpace = (size1 / (1024)) + " Mo";
}
else {
rslt_freeSpace = size1 + " ko";
}
//calcule Statistique disque dur
stat_Final = Double.valueOf(rslt_freeSpace.replaceAll(" Go", ""))/Double.valueOf(rslt_totalSpace.replaceAll(" Go", ""))*100;
/**
* Conversion 2 chiffres après la virgule
*/
DecimalFormat df = new DecimalFormat("########.00");
rslt_F = (df.format(stat_Final));
//debug
System.out.println("Propriété du fichier : " + f1);
System.out.println("Taille de l'espace libre : " + rslt_freeSpace);
System.out.println("Taille de l'espace du disque: " + rslt_totalSpace);
System.out.println("Stat de l'espace du disque : " + rslt_F+ " " + "%" + "\n");
}
}
}
}
} |
Partager