package com.javarush.task.task31.task3113; import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; import java.util.*; import static java.lang.System.out; /* Что внутри папки? Что внутри папки? Напиши программу, которая будет считать подробную информацию о папке и выводить ее на консоль. Первым делом считай путь к папке с консоли. Если введенный путь не является директорией — выведи «[полный путь] — не папка» и заверши работу. Затем посчитай и выведи следующую информацию: Всего папок — [количество папок в директории] Всего файлов — [количество файлов в директории и поддиректориях] Общий размер — [общее количество байт, которое хранится в директории] Используй только классы и методы из пакета java.nio. */ public class Solution { static int countPapok =0; static int countFiles =0; public static void main(String[] args) throws IOException { Scanner scan = new Scanner(System.in); String path = scan.nextLine(); Path pathFile = Paths.get(path); if(!Files.isDirectory(pathFile)) { out.println(pathFile.toAbsolutePath()+" — не папка"); return; } else { Files.walkFileTree(pathFile,new SimpleFileVisitor<Path>(){ @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { countFiles++; return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { countPapok++; return FileVisitResult.CONTINUE; } }); } out.println("Всего папок — "+(countPapok-1)); out.println("Всего файлов — "+countFiles); out.println("Общий размер — "+Files.size(pathFile)); scan.close(); } } 

Tell me, please, what is the problem (does not pass the validator). It seems that everything works (maybe I incorrectly count the number of folders?). Recursion cannot be used.

  • one
    I advise you to wrap the Scaner in try-with-resources . It is not clear what kind of validator? And what do you mean by no recursion? After all, walkFileTree - recursively bypasses. Maybe you meant without cyclic recursions? - And
  • one
    Files.size returns the size of the file from the argument; for the folder, the result is undefined, but it is definitely not equal to the sum of all contained within the files. - zRrr
  • Clearly, I mean where I count the number of files in parallel, should I take their weight and add it to the total weight variable? - Dualist

0