Skip to content

Instantly share code, notes, and snippets.

@snarkbait
Created January 11, 2016 05:05
Show Gist options
  • Save snarkbait/a1cb1667ee8098020b27 to your computer and use it in GitHub Desktop.
Save snarkbait/a1cb1667ee8098020b27 to your computer and use it in GitHub Desktop.
package enumexample;
import javax.swing.*;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
/**
* @author /u/Philboyd_Studge on 1/10/2016.
*/
public class ConsumerExample extends JFrame {
JPanel panel = new JPanel();
JProgressBar bar = new JProgressBar();
Path path;
int fileCount;
public ConsumerExample(String pathName) throws IOException {
path = Paths.get(pathName);
getFileCount();
bar.setMaximum(fileCount);
this.setSize(400,300);
bar.setStringPainted(true);
panel.add(bar);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setContentPane(panel);
this.setVisible(true);
}
public void getFileCount() throws IOException {
Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
fileCount++;
return super.visitFile(file, attrs);
}
});
System.out.println(fileCount);
}
public void updateBar(int value) {
bar.setValue(bar.getValue() + value);
}
public void updateBarString(String name) {
updateBar(1);
bar.setString(name);
}
public static void main(String[] args) throws IOException {
ConsumerExample ce = new ConsumerExample("C:/BlitzPlus");
FileVisitorConsumer fv = new FileVisitorConsumer(ce::updateBarString);
Files.walkFileTree(ce.path,fv);
}
}
package enumexample;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.Consumer;
/**
* @author /u/Philboyd_Studge on 1/10/2016.
*/
public class FileVisitorConsumer extends SimpleFileVisitor<Path> {
Consumer<String> consumer;
public FileVisitorConsumer(Consumer<String> consumer) {
super();
this.consumer = consumer;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
consumer.accept(file.getFileName().toString());
try {
Thread.sleep(50); //adjust this or remove
} catch (InterruptedException e) {
e.printStackTrace();
}
return super.visitFile(file, attrs);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment