Last Updated : 23 Jul, 2025
A directory is an organizational file system structure that contains Files and Directorates. Even an attacker can try to traverse or access a folder which we name as 'File Traversal Attack or Path Traversal Attack a different directory. In short here the directory is traversed which is outside the home/root directory. These files are server-internal files that are not accessible by the user.
Traversal AttacksThe prerequisites required are listFiles() and considering there are no path traversal attacks.
Different Ways to traverse in a DirectoryMethod 1: Using listFiles() Method of File class
Suppose there exists a directory with path C:\\GFG. The following image displays the files and directories present inside GFG folder. The subdirectory "Ritik" contains a file named "Logistics.xlsx" and the subdirectory "Rohan" contains a file named "Payments.xlsx".
GFG DirectoryApproach:
Example:
Java
// Java Program to Traverse Through a Directory
// Importing required classes
import java.io.File;
// Main class
class GFG {
// Method 1
// To display files
public static void displayFiles(File[] files)
{
// Traversing through the files array
for (File filename : files) {
// If a sub directory is found,
// print the name of the sub directory
if (filename.isDirectory()) {
System.out.println("Directory: "
+ filename.getName());
// and call the displayFiles function
// recursively to list files present
// in sub directory
displayFiles(filename.listFiles());
}
// Printing the file name present in given path
else {
// Getting the file name
System.out.println("File: "
+ filename.getName());
}
}
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Storing the name of files and directories
// in an array of File type
File[] files = new File("C:\\GFG").listFiles();
// Calling method 1 to
// display files
displayFiles(files);
}
}
Output:
File: article.docx File: GFG.txt File: numbers.txt Directory: Ritik File: Logistics.xlsx Directory: Rohan File: Payments.xlsx
Method 2: Using walk() method in Java 8 and onwards
Java 8 onwards, the walk() method was introduced to iterate over the entire directory recursively and retrieve Stream<Path> as the return value.
Approach:
Example:
Java
// Java Program to Display Files with
// Complete Path Present in a Directory
// Importing required classes
import java.io.*;
import java.nio.file.*;
import java.util.stream.Stream;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
throws IOException
{
// Creating try-catch block and
// providing the directory path of local machine
try (Stream<Path> filepath
= Files.walk(Paths.get("c:\\GFG")))
{
// Printing the name of directories and files
// with entire path
filepath.forEach(System.out::println);
}
// Catch block to handle exceptions
catch (IOException e) {
// If no such directory exists throw an
// exception
throw new IOException("Directory Not Present!");
}
}
}
Output:
c:\GFG c:\GFG\article.docx c:\GFG\GFG.txt c:\GFG\numbers.txt c:\GFG\Ritik c:\GFG\Ritik\Logistics.xlsx c:\GFG\Rohan c:\GFG\Rohan\Payments.xlsx
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4