Skip to content

Instantly share code, notes, and snippets.

@rchrand
Created February 25, 2013 09:40
Show Gist options
  • Save rchrand/5028759 to your computer and use it in GitHub Desktop.
Save rchrand/5028759 to your computer and use it in GitHub Desktop.
Java searching recursive
public class SearchSize extends SwingWorker<Long, Long> {
private String currentPath;
private JLabel lblSize;
private long totalSize = 0;
public SearchSize(String path, JLabel lblSize){
this.currentPath = path;
this.lblSize = lblSize;
}
@Override
protected Long doInBackground() throws Exception {
totalSize = 0;
File file = new File(currentPath);
for (File f : file.listFiles()){
System.out.println(f.getAbsolutePath());
if (f.isFile()){
totalSize += f.length();
publish(totalSize);
}
else if(f.isDirectory()) {
totalSize(currentPath+"/"+f.getName());
}
}
return totalSize;
}
@Override
protected void process(List<Long> chunks) {
System.out.println(chunks);
System.out.println(lblSize);
lblSize.setText("Total size of files: " + chunks.get(chunks.size()-1)/1024 + " KB");
}
public void totalSize(String path){
File file = new File(path);
for (File f : file.listFiles()){
System.out.println(f.getAbsolutePath());
if (f.isFile()){
totalSize += f.length();
publish(totalSize);
}
else if(f.isDirectory()) {
totalSize(path+"/"+f.getName());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment