Skip to content

Instantly share code, notes, and snippets.

@legap
Last active April 12, 2016 13:31
Show Gist options
  • Save legap/0e8eb2b550ecd6d533576a5e59bb9657 to your computer and use it in GitHub Desktop.
Save legap/0e8eb2b550ecd6d533576a5e59bb9657 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.FileFilter;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class JavaFileOperations {
public void findAllXmlFilesInFolderWithJavaIO() {
String myPath = "your/path/goes/here";
File folder = new File(myPath);
FileFilter fileFilter = pathname -> pathname.isFile() && pathname.getName().endsWith(".xml");
File[] xmlFiles = folder.listFiles(fileFilter);
for (int i = 0; i < xmlFiles.length; i++) {
System.out.println(xmlFiles[i].getAbsoluteFile());
}
}
// FIXME filter for xml files only
public void findAllFilesInFolderWithJavaNIO() throws Exception {
Path myPath = Paths.get("your/path/goes/here");
DirectoryStream<Path> stream = Files.newDirectoryStream(myPath);
for (Path entry : stream) {
System.out.println(entry.getFileName());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment