I want to get the size of a folder and also the size of files in that folder but the output is zero even though I used the java long method for file size and a for loop to access the files in the folder.The programme only outputs the size of individual files (those that are not stored in any folder) and gives the folder size and the size of files in the folder as zero.How can I fix this so that the code reads the folder size and also the size of files in that folder?
import java.io.*;
public class FileSize{
public static void main(String[] args) throws IOException{
System.out.print("Please Enter file or Folder name : ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
File f = new File(in.readLine());
if(f.isFile()) {
long file_size = f.length();
System.out.println("File Name Is:" +f.getName());
System.out.println("File Size:" +file_size+ "Bytes");
}
else if(f.isDirectory()){
long file_size = f.length();
File[] filesInDirectory = f.listFiles();
System.out.println("Size Of The Directory is:" +file_size);
System.out.println("The Files in the Directory:");
for(int i =1;i<filesInDirectory.length; i++)
{
System.out.println("Name Of File Number " +i+" Is:"+filesInDirectory[i]);
System.out.println("Size In Bytes Is:"+file_size);
}
}
else{
System.out.println("File does not exist");
}
}
}