Skip to content

Instantly share code, notes, and snippets.

@embs
Created June 20, 2012 16:44
Show Gist options
  • Save embs/2960865 to your computer and use it in GitHub Desktop.
Save embs/2960865 to your computer and use it in GitHub Desktop.
Código base da prova prática de PLC(~if686) - Java Concorrente
import java.io.File;
public class ImageSizeCalculator {
private long totalBytes = 0;
private long numFiles = 0;
// Tamanho do maior arquivo que não é um diretório.
private long sizeOfLargestFile = 0;
private void getTotalSize(File file) {
long thisFile = 0;
if (isImage(file)) {
thisFile = file.length();
numFiles++;
totalBytes += thisFile;
}
File[] children = file.listFiles();
// se file for um diretório, procede recursivamente.
if (children != null) {
for (File child : children) {
getTotalSize(child);
}
}
else { //Não é um diretório.
if (thisFile > sizeOfLargestFile) {
sizeOfLargestFile = thisFile;
}
}
}
private boolean isImage(File file) {
if (file.isFile()
&& (file.getName().toLowerCase().endsWith(".jpg") ||
file.getName().toLowerCase().endsWith(".jpeg") ||
file.getName().toLowerCase().endsWith(".png"))) {
return true;
}
return false;
}
public static void main(String[] args) {
long start = System.nanoTime();
ImageSizeCalculator counter = new ImageSizeCalculator();
// Faz a medição a partir do diretório atual. Isso NÃO precisa mudar.
counter.getTotalSize(new File("."));
long end = System.nanoTime();
System.out.println("Total: " + counter.totalBytes + " bytes");
System.out.println("O maior arquivo tem : " + counter.sizeOfLargestFile + " bytes");
System.out.println("Número de arquivos: " + counter.numFiles);
System.out.println("Tempo gasto: " + (end - start) / 1.0e9 + "s");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment