Java 8 walk How to Read all files in a folder - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, I am going to show you how to read all files in a folder using Java 8 walk syntax.
Java 7 introduced Files class that consists exclusively of static methods that operate on files, directories, or other types of data.
From Java 8 Walk method was introduced as part of Files class which is used to read the files under the specified path or directory.
Java 8 Walk takes three parameters :
start– The starting file.
maxDepth – The maximum number of directory levels to visit.
Options – options to configure the traversal.
And returns Stream of Path (Stream<Path>) it contains all available files under given start point.
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class Java8ReadFiles {

 public static void main(String[] args) {
  try (Stream < Path > paths = Files.walk(Paths.get("C:\\DemoFolder"))) {
   paths
    .filter(Files::isRegularFile)
    .forEach(System.out::println);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

}
It will print all regular files under the given start point. On the above example, we have used Java 7’s most valuable feature try with the resource it is also called as Automatic Resource Management.
import java.io.File;

public class Java8ReadFiles {

    public static void main(String[] args) {
        try {
            final File folder = new File("C:\\DemoFolder");
            listAllFiles(folder);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void listAllFiles(final File folder) {
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                listAllFiles(fileEntry);
            } else {
                System.out.println(fileEntry.getName());
            }
        }
    }
}
 
Files.walk
ListFiles
Happy Learning 🙂

How to get the size of a Directory in Python ?
Python – How to create Zip File in Python ?
How to get Stream count in Java 8
How to Merge Streams in Java 8
How to Convert Iterable to Stream Java 8
Java 8 Read File Line By Line Example
Java – How to read CSV file and Map to Java Object
How to add dynamic files to JTree
How to Copy Local Files to AWS EC2 instance Manually ?
How add files to S3 Bucket using Shell Script
Java 8 How to convert Stream to List
How to Filter a Map in Java 8
User defined sorting with Java 8 Comparator
Java 8 groupingBy Example
Java 8 how to remove duplicates from list

How to get the size of a Directory in Python ?
Python – How to create Zip File in Python ?
How to get Stream count in Java 8
How to Merge Streams in Java 8
How to Convert Iterable to Stream Java 8
Java 8 Read File Line By Line Example
Java – How to read CSV file and Map to Java Object
How to add dynamic files to JTree
How to Copy Local Files to AWS EC2 instance Manually ?
How add files to S3 Bucket using Shell Script
Java 8 How to convert Stream to List
How to Filter a Map in Java 8
User defined sorting with Java 8 Comparator
Java 8 groupingBy Example
Java 8 how to remove duplicates from list
 


Δ
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String