kasem1910

Untitled

May 9th, 2021
734
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package gradleproject3;
  7.  
  8. import java.io.File;
  9. import java.util.Scanner;
  10. public class DirectorySize {
  11.  public static void main(String[] args) {
  12.  // Prompt the user to enter a directory or a file
  13.  System.out.print("Enter a directory or a file: ");
  14.  Scanner input = new Scanner(System.in);
  15.  String directory = input.nextLine();
  16.  // Display the size
  17.  System.out.println(getSize(new File(directory)) + " bytes");
  18.  }
  19. public static double getSize(File file_){
  20.     if(file_.isFile()){
  21.         return file_.length();
  22.     }else{
  23.     File contents[] = file_.listFiles();
  24.      double all_files_size = 0.0;
  25.       for(int i=0; i<contents.length; i++) {
  26.          all_files_size += getSize(contents[i]);
  27.       }
  28.       return all_files_size;
  29.     }
  30.    
  31. }
  32. }
  33.  
RAW Paste Data