Advertisement
desislava_topuzakova

8. Get Folder Size

Jan 22nd, 2022
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. package StreamsFilesDirectories_Exercise;
  2.  
  3. import java.io.File;
  4. import java.nio.file.Files;
  5.  
  6. public class GetFolderSize_08 {
  7.     public static void main(String[] args) {
  8.         //1. достъп до папката -> път
  9.         //2. обхождаме всички файлове в папката -> всеки файл взимаме размера -> сумираме
  10.         //3. печатаме общия размер на папката
  11.         String path = "C:\\Users\\I353529\\Desktop\\04. Java-Advanced-Files-and-Streams-Exercises-Resources\\Exercises Resources";
  12.         File folder = new File(path);
  13.         File[] allFiles = folder.listFiles();
  14.  
  15.         int folderSize = 0; //размер на папката
  16.         for (File file : allFiles) {
  17.             folderSize += file.length();
  18.         }
  19.  
  20.         System.out.println("Folder size: " + folderSize);
  21.     }
  22. }
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement