Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java function that takes a directory path, and prints the size, in bytes

ID: 3728715 • Letter: W

Question

Write a Java function that takes a directory path, and prints the size, in bytes, of all the files, ignoring sub-directories, reverse sorted by size, with the total in the following exact format:
File_name_2: 200000 File_name_1: 100000
File_name_3: 1000

>3 File(s) - Total: 301000 Write a Java function that takes a directory path, and prints the size, in bytes, of all the files, ignoring sub-directories, reverse sorted by size, with the total in the following exact format:
File_name_2: 200000 File_name_1: 100000
File_name_3: 1000

>3 File(s) - Total: 301000
File_name_2: 200000 File_name_1: 100000
File_name_3: 1000

>3 File(s) - Total: 301000

Explanation / Answer

import java.io.File;

import java.util.Scanner;

public class DirectoryFileSize {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       System.out.print("Enter directory: ");

       String dir = sc.next();

       sc.close();

       File directory = new File(dir);

      

       if(directory.exists() && directory.isDirectory()) {

           long length = 0;

           int count = 0;

           for (File file : directory.listFiles()) {

               if (file.isFile()){

                   count++;

                   length += file.length();

                   System.out.println(file+": "+file.length());

               }      

           }

          

           System.out.println(count+" Files(s) - Total: "+length);

       }

   }

}

/*

Sample run:

Enter directory: .

./TimeTables.txt: 918

./42.87 23.33 2.10 EXC: 275

./scores.txt: 4355

./grade.txt: 29

./SampleFile.json: 312

./int_data.txt: 469

./Lab02Report.txt: 614

./binaryGrade.dat: 30

./inputData.txt: 34

./Lab09Report.txt: 1743

./.DS_Store: 6148

./Test1.txt: 24

./arraysort.cpp: 2314

./mydata.txt: 472

./XXData.txt: 72

./Lab03ArrayFile.txt: 275

./overloading_abs.cpp: 490

./temperature.txt: 89

./hs_err_pid11370.log: 37934

./ExamStatFile.txt: 125

./numbers.dat: 24

./fruits.txt: 73

./months.txt: 86

./Lab09Names.txt: 393

./XXOutput.txt: 78

./difference.txt: 4

./number.bin: 20

./outDataFile.txt: 118

./students.txt: 106

./infix.txt: 145

./BoyNames.txt: 75

./Lab09StudentFile.txt: 570

./chapter10numbers.dat: 26

./.classpath: 514

./results.txt: 53

./c.txt: 201

./students.txt.average: 102

./b.txt: 127

./temperatures.dat: 4616

./TestPerson.java: 1299

./desc.txt: 0

./asc.txt: 0

./.project: 364

./primes_30.txt: 68

./text.txt: 102

./integers.txt: 40

./H5.in: 54

./emaillist.txt: 81

./Lab02Stocks.txt: 31

./canvas.h: 2260

./ency.txt: 229

./input.dat: 85

./output.txt: 18

./data.txt: 61

./String.txt: 25

55 Files(s) - Total: 68770

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote