Я ищу для поиска всех файлов/папок в заданном каталоге до определенной глубины. Это мой код до сих порРекурсивно печатать все файлы в папке до определенной глубины
import java.io.File;
import java.util.Scanner;
/*
* Michael Woloski - Program Three
*
* This program allows the user to enter a desired path
* then the program will display every file or directory
* within the specified path. The user will also enter a
* desired depth, so if the path contains multiple
* directories, it will display the files/folders in the sub
* directory
*/
public class MainClass {
static Scanner sc = new Scanner(System.in);
public static void fileListingMethod(File [] files, int depth)
{
if(depth == 0)
{
return;
}
else
{
for(File file : files)
{
if(file.isDirectory())
{
System.out.printf("Parent: %s\n", file.getParent());
System.out.printf(" Directory: %s\n", file.getName());
fileListingMethod(file.listFiles(), depth--);
}
else
{
System.out.printf(" File: %s\n", file.getName());
}
}
}
}
public static void main(String [] args)
{
System.out.printf("Please Enter a Desired Directory: ");
String g_input = sc.nextLine();
if(new File(g_input).isDirectory())
{
System.out.printf("Please Enter Desired Depth: ");
int depth = sc.nextInt();
File [] file = new File(g_input).listFiles();
fileListingMethod(file, depth);
}
else
{
System.out.printf("The path %s is not a valid entry. Exiting. ", g_input);
System.exit(0);
}
}
}
Однако, если пользователь вводит 3 в глубину он сканирует все папки/файлы в течение первых трех папок в каталоге.
По существу, я хочу получить файлы/папки из одной директории до нужной глубины.
'если пользователь вводит 3 в качестве глубины, он сканирует всю папку/файлы в первых трех папках'. Глубина означала бы, сколько вложенных папок оно опустится, а не сколько папок, которые он проверит. –
Да, например, я проверяю папку с загрузками C: \ Users \ Name \ Downloads и у меня есть 4 папки в этом каталоге, если я ввожу 3, только 3 из 4 папок фактически получают петлю, и когда они показывают, они показывают каждый файл или подпапку в папке и продолжайте до тех пор, пока окончательный файл не будет распечатан. – ski6154