Select all true statements import java.io.File; import java.util.Scanner; public class DirectorySize { public static void main(String[] args) { // Prompt the user to enter a directory or a file System.out.print("Enter a directory or a file: "); Scanner input = new Scanner(System.in); String directory = input.nextLine(); // Display the size System.out.println(getSize(new File(directory)) + " bytes"); } public static long getSize(File file) { long size = 0; // Store the total size of all files if (file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; files != null && i < files.length; i++) { size += getSize(files[i]); // Recursive call } } else { // Base case size += file.length(); } return size; } } a. The length() method returns the size of a file. b. The listFiles() method returns an array of File objects and excludes each directory. c. The length() method returns a value of -1 if the file is a directory. d. The listFiles() method returns an array of File objects under a directory.
Select all true statements
import java.io.File;
import java.util.Scanner;
public class DirectorySize {
public static void main(String[] args) {
// Prompt the user to enter a directory or a file
System.out.print("Enter a directory or a file: ");
Scanner input = new Scanner(System.in);
String directory = input.nextLine();
// Display the size
System.out.println(getSize(new File(directory)) + " bytes");
}
public static long getSize(File file) {
long size = 0; // Store the total size of all files
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; files != null && i < files.length; i++) {
size += getSize(files[i]); // Recursive call
}
}
else { // Base case
size += file.length();
}
return size;
}
}
a. |
The length() method returns the size of a file. |
|
b. |
The listFiles() method returns an array of File objects and excludes each directory. |
|
c. |
The length() method returns a value of -1 if the file is a directory. |
|
d. |
The listFiles() method returns an array of File objects under a directory. |

Trending now
This is a popular solution!
Step by step
Solved in 3 steps









