public class Solution {
public static List<String> getFileTree(String root) throws IOException {
List<String> listFiles = new ArrayList<>();
File folder = new File(root);
Queue<File> queue = new LinkedList<>();
queue.offer(folder);
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
queue.offer(file);
}
}
for (int i = 0; i < queue.size(); ) {
for (File file : queue.poll().listFiles()) {
if (file.isFile()) {
listFiles.add(file.getName());
}
}
}
return listFiles;
}
public static void main(String[] args) throws IOException {
String root = new String("c:\\test\\");
getFileTree(root);
}
}
package com.javarush.task.task31.task3102;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
/*
Находим все файлы
*/
public class Solution {
public static List<String> getFileTree(String root) throws IOException {
List<String> listFiles = new ArrayList<>();
File folder = new File(root);
Queue <File> queue = new LinkedList<>();
queue.offer(folder);
// Получим каталоги, которые есть в root
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
queue.offer(file);
}
}
// Переберем каталоги в стеке и заполним список
for (int i = 0; i < queue.size(); ) {
for (File file : queue.poll().listFiles()) {
if (file.isFile()) {
listFiles.add(file.getName());
}
}
}
for (int i = 0; i < listFiles.size(); i++) {
System.out.print(listFiles.get(i) + " , ");
}
return listFiles;
}
public static void main(String[] args) throws IOException {
String root = new String("c:\\test\\");
getFileTree(root);
}
}