programa Java para recorrer todos los archivos de un directorio / carpeta

En este programa java, vamos a aprender cómo acceder y recorrer todos los archivos de un directorio / carpeta ? En este caso, estamos atravesar todos los archivos de un directorio dado .

Dado un directorio (carpeta) y tenemos que atravesar sus todos los archivos mediante el programa Java.

Ejemplo:

Input:
Enter path: ‘E:Java’
(Java folder in ‘E’ drive)
Output:
About to traverse the directory: JAVA
About to traverse the directory: 1 Nov
Visiting file:ChristmasTree.java
Visiting file:Find Files in Zip File.docx
Finished with the directory: 1 Nov
About to traverse the directory: 2 Nov
Visiting file:Combine Path.docx
Visiting file:CombinePath.java
Finished with the directory: 2 Nov
About to traverse the directory: 3 Nov
Visiting file:Check absolute path.docx
Visiting file:CheckAbsolutePath.java
Finished with the directory: 2 Nov

Programa para recorrer todos los archivos de un directorio en Java

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Scanner;
public class TraverseFileSystem
{
public static void main(String[] args)
{
try
{
// create object of scanner.
Scanner KB=new Scanner(System.in);
// enter path here.
System.out.print("Enter path here : ");
String A=KB.next();
Path path = Paths.get(A);
ListFiles listFiles = new ListFiles();
Files.walkFileTree(path, listFiles);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
class ListFiles extends SimpleFileVisitor<Path>
{
private final int indentionAmount = 3;
private int indentionLevel;
public ListFiles()
{
indentionLevel = 0;
}
private void indent()
{
for (int i = 0; i < indentionLevel; i++)
{
System.out.print(' ');
}
}
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes)
{
indent();
System.out.println("Visiting file:" + file.getFileName());
return FileVisitResult.CONTINUE;
}
public FileVisitResult postVisitDirectory(Path directory, IOException e)throws IOException
{
indentionLevel = indentionAmount;
indent();
System.out.println("Finished with the directory: "+ directory.getFileName());
return FileVisitResult.CONTINUE;
}
public FileVisitResult preVisitDirectory(Path directory,BasicFileAttributes attributes) throws IOException
{
indent();
System.out.println("About to traverse the directory: "+ directory.getFileName());
indentionLevel += indentionAmount;
return FileVisitResult.CONTINUE;
}
public FileVisitResult visitFileFailed(Path file, IOException exc)throws IOException
{
System.out.println("A file traversal error ocurred");
return super.visitFileFailed(file, exc);
}
}

salida

Enter path here : E:/JAVA
About to traverse the directory: JAVA
About to traverse the directory: 1 Nov
Visiting file:ChristmasTree.java
Visiting file:Find Files in Zip File.docx
Finished with the directory: 1 Nov
About to traverse the directory: 2 Nov
Visiting file:Combine Path.docx
Visiting file:CombinePath.java
Finished with the directory: 2 Nov
About to traverse the directory: 3 Nov
Visiting file:Check absolute path.docx
Visiting file:CheckAbsolutePath.java
Finished with the directory: 2 Nov


Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *