I test my code well on the local machine, how ever, it failed on the website and even didn't show which condition I hadn't match!
Wired...
package com.codegym.task.task31.task3113;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
/*
What's in the folder?
*/
public class Solution {
public static void main(String[] args) throws IOException {
readPath();
}
private static void readPath() throws IOException {
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
String line = console.readLine();
console.close();
Path path = Paths.get(line);
if (!Files.isDirectory(path)) {
System.out.println(line + " is not a folder");
System.exit(0);
}
else
calcTotal(path);
}
private static void calcTotal(Path path) throws IOException {
List<Integer> folderCount = new ArrayList<>();
List<Integer> fileCount = new ArrayList<>();
List<Long> fileSize = new ArrayList<>();
int totalFolder;
int totalFile;
long totalFileSize = 0L;
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
folderCount.add(1);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
fileCount.add(1);
File currentFile = new File(file.toString());
fileSize.add(currentFile.length());
return FileVisitResult.CONTINUE;
}
});
totalFolder = folderCount.size() - 1;
totalFile = fileCount.size();
for (long size : fileSize)
totalFileSize += size;
System.out.println("Total folders: " + totalFolder);
System.out.println("Total files: " + totalFile);
System.out.println("Total size: " + totalFileSize);
}
}