1. Overview
In this programming tutorial, we are showing an example program on how to list or traverse all files and folders including subfolders in a directory using the following methods.
A) Using the classic approach
B) Using Java 8 methods
Java 12 Files mismatch Method Example to Compare two Files
2. Using the classic approach
This is the most commonly used by java developers before java 8. This program is implemented using the Recursive approach.
package com.javaprogramto.w3schools.programs.files; import java.io.File; import java.util.ArrayList; import java.util.List; public class ListAllFIlesClassic { public static void main(String[] args) { File folder = new File( "java\\com\\javaprogramto\\w3schools\\programs"); List<String> fileNamesList = new ArrayList<>(); traverseDirectory(".*\\.java", folder, fileNamesList); for (String fileName : fileNamesList) { System.out.println(fileName); } } public static void traverseDirectory(final String pattern, final File folder, List<String> fileNamesList) { for (final File f : folder.listFiles()) { if (f.isDirectory()) { traverseDirectory(pattern, f, fileNamesList); } if (f.isFile()) { if (f.getName().matches(pattern)) { fileNamesList.add(f.getAbsolutePath()); } } } } }
Output:
Java 8 introduced a method walk() in Files class.
Return a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file. The file tree is traversed depth-first, the elements in the stream are Path objects that are obtained as if by resolving the relative path against start.
We can find the files and directories using Files.walk() method.
Output:
In this short article, we have seen the ways to traverse all the files and folder folders including subfolders.
Read next on - How to get the current working directory in java in different ways?
java\com\javaprogramto\w3schools\programs\files\ListAllFIlesClassic.java java\com\javaprogramto\w3schools\programs\files\ReadLineByLineBufferedReader.java java\com\javaprogramto\w3schools\programs\files\ReadLineByLineFiles.java java\com\javaprogramto\w3schools\programs\files\ReadLineByLineRandomAccessFile.java java\com\javaprogramto\w3schools\programs\files\ReadLineByLineScanner.java java\com\javaprogramto\w3schools\programs\list\ArrayListCopyAddAll.java java\com\javaprogramto\w3schools\programs\list\ArrayListCopyCollectionsCopy.java java\com\javaprogramto\w3schools\programs\list\ArrayListCopyContructor.java java\com\javaprogramto\w3schools\programs\list\ArrayListCopyJava8.java
3. Using Java 8
Java 8 introduced a method walk() in Files class.
Return a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file. The file tree is traversed depth-first, the elements in the stream are Path objects that are obtained as if by resolving the relative path against start.
We can find the files and directories using Files.walk() method.
package com.javaprogramto.w3schools.programs.files; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class ListAllFIlesJava8 { public static void main(String[] args) { String directory = "java\\com\\javaprogramto\\w3schools\\programs"; // Finding only files. System.out.println("File Names : "); printFileNames(directory); // Finding only directories System.out.println("Folder Names : "); printFolderNames(directory); // Filtering file names by a pattern. System.out.println("Filtering name by a pattern \"Line\": "); filterByPattern(directory); } private static void printFileNames(String directory) { // Reading the folder and getting Stream. try (Stream<Path> walk = Files.walk(Paths.get(directory))) { // Filtering the paths by a regualr file and adding into a list. List<String> fileNamesList = walk.filter(Files::isRegularFile).map(x -> x.toString()) .collect(Collectors.toList()); // printing the file nams fileNamesList.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } private static void printFolderNames(String directory) { // Reading the folder and getting Stream. try (Stream<Path> walk = Files.walk(Paths.get(directory))) { // Filtering the paths by a folder and adding into a list. List<String> folderNamesList = walk.filter(Files::isDirectory).map(x -> x.toString()) .collect(Collectors.toList()); // printing the folder names folderNamesList.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } private static void filterByPattern(String directory) { // Reading the folder and getting Stream. try (Stream<Path> walk = Files.walk(Paths.get(directory))) { // Filtering the paths by a folder and adding into a list. List<String> fileNamesList = walk.map(x -> x.toString()).filter(f -> f.contains("Line")) .collect(Collectors.toList()); // printing the folder names fileNamesList.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } }
Output:
File Names : java\com\javaprogramto\w3schools\programs\files\ListAllFIlesClassic.java java\com\javaprogramto\w3schools\programs\files\ListAllFIlesJava8.java java\com\javaprogramto\w3schools\programs\files\ReadLineByLineBufferedReader.java java\com\javaprogramto\w3schools\programs\files\ReadLineByLineFiles.java java\com\javaprogramto\w3schools\programs\files\ReadLineByLineRandomAccessFile.java java\com\javaprogramto\w3schools\programs\files\ReadLineByLineScanner.java java\com\javaprogramto\w3schools\programs\list\ArrayListCopyAddAll.java java\com\javaprogramto\w3schools\programs\list\ArrayListCopyCollectionsCopy.java java\com\javaprogramto\w3schools\programs\list\ArrayListCopyContructor.java java\com\javaprogramto\w3schools\programs\list\ArrayListCopyJava8.java Folder Names : java\com\javaprogramto\w3schools\programs java\com\javaprogramto\w3schools\programs\files java\com\javaprogramto\w3schools\programs\list Filtering name by a pattern "Line": java\com\javaprogramto\w3schools\programs\files\ReadLineByLineBufferedReader.java java\com\javaprogramto\w3schools\programs\files\ReadLineByLineFiles.java java\com\javaprogramto\w3schools\programs\files\ReadLineByLineRandomAccessFile.java java\com\javaprogramto\w3schools\programs\files\ReadLineByLineScanner.java
4. Conclusion
In this short article, we have seen the ways to traverse all the files and folder folders including subfolders.
No comments:
Post a Comment
Please do not add any spam links in the comments section.