Created
January 11, 2016 05:05
-
-
Save snarkbait/a1cb1667ee8098020b27 to your computer and use it in GitHub Desktop.
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
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); | |
} | |
} |
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
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