Skip to content

Instantly share code, notes, and snippets.

@easikoglu
Last active August 29, 2015 14:21
Show Gist options
  • Save easikoglu/e53f41a49e8aa9f9002f to your computer and use it in GitHub Desktop.
Save easikoglu/e53f41a49e8aa9f9002f to your computer and use it in GitHub Desktop.
Traverse through file directory
package com.easikoglu.blog;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.Date;
/**
* Created with IntelliJ IDEA.
* User: eerhasi
* Date: 30.12.2013
* Time: 22:48
* To change this template use File | Settings | File Templates.
*/
public class HelloWorld {
public void visitFiles(File node) {
if (!node.isDirectory()) {
System.out.println("File Name : "+ node.getName());
}
if (node.isDirectory()) {
String[] subNote = node.list();
for (String filename : subNote) {
visitFiles(new File(node, filename));
}
}
}
public void visitFiles(String directoryPath) {
try {
Path startPath = Paths.get(directoryPath);
Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
try {
FileTime lastModifiedTime = Files.getLastModifiedTime(file);
Date date = new Date(lastModifiedTime.toMillis());
System.out.println("File Name : "+ file.getFileName().toString() + " last modified date :" +date);
} catch (IOException e) {
e.printStackTrace();
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
//ignore
}
}
public void visitFiles8(String path){
Files.walk(Paths.get(path))
.filter(Files::isRegularFile)
.forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment