import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class DiskSpaceCheck {
FileWriter dscp;
public DiskSpaceCheck() {
}
public void DiskSpaceCheckPage(){
try {
this.DiskSpaceCheckLoop("C:\\DiskSpacePage.html");
} catch (IOException e) {e.printStackTrace();
} catch (InterruptedException e) {e.printStackTrace();
}
}
public void DiskSpaceCheckLoop(String fileName) throws IOException, InterruptedException {
while(true){
this.openFile(fileName);
for(int i=0; i<File.listRoots().length;i++){
this.newCell();
this.DiskSpace(File.listRoots()[i]+"");
this.closeCell();
}
System.out.println("write");
this.closeFile();
Thread.sleep(2000);
}
}
public void DiskSpace(String directory) throws IOException {
File file = new File(directory);
this.writeOnFile("<b>" +directory + "</b>");
double usable = file.getUsableSpace()/(1024*1024*1024);
double total = file.getTotalSpace()/(1024*1024*1024);
String um = "";
if (total > 1) um ="GB";
else um ="MB";
this.writeOnFile("Total: " + total + " " + um);
//System.out.println("Free: " + file.getFreeSpace());
if (usable > 1) um ="GB";
else um ="MB";
this.writeOnFile("Usable: " + usable + " " + um);
double ratio = usable/total;
ratio = this.approx(ratio*100,2);
//System.out.println("Ratio: " + (ratio) + "%");
this.writeOnFile("Ratio: " + (ratio) + "%\n");
}
public static void main(String[] args) {
DiskSpaceCheck dsc = new DiskSpaceCheck();
dsc.DiskSpaceCheckPage();
}
void newCell() throws IOException {
this.dscp.write("<td>");
}
void closeCell() throws IOException {
this.dscp.write("</td>");
}
void writeOnFile(String string) throws IOException {
this.dscp.write(string + "<br>");
}
void openFile(String fileName) throws IOException {
this.dscp = new FileWriter(fileName);
this.dscp.write("<html>\n<body>\n");
this.dscp.write("<title>Disk Space Check</title>\n");
this.dscp.write("<table><tr>\n");
}
void closeFile() throws IOException {
this.dscp.write("</tr></table></body>\n</html>\n");
this.dscp.flush();
this.dscp.close();
}
double approx(double variabile, int decimali){
double intero = Math.floor(variabile);
double decimale = 0;
decimale = variabile - intero ;
decimale = (Math.round(decimale * Math.pow(10,2)))/Math.pow(10,2); //Se vuoi l'approssimazione a 3 cifre, usa 1000 al posto di 100
variabile = intero + decimale;
return variabile;
}
}