Created
February 25, 2013 09:40
-
-
Save rchrand/5028759 to your computer and use it in GitHub Desktop.
Java searching recursive
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
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