Question

Write a Java program that lists all the files in a directory and their subdirectories, that...

Write a Java program that lists all the files in a directory and their subdirectories, that mimics the Unix ls command or the Windows dir command. Note that when a directory is encountered we do not immediately print its contents recursively. Rather, as we scan each directory, place any subdirectory in a List. After the directory entries are printed then process each subdirectory recursively. For each file that is listed, include the modification time, the file size, and if it is a directory, have your output say so. For each directory, print the complete directory name prior to printing its contents.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Main.java

import java.io.File;
  
public class Main
{
static void SubDirectories(File[] fl, int lvl)
{
for (File z : fl)
{
for (int x = 0; x < lvl; x++)
System.out.print("\t");
  
if(z.isFile())
System.out.println(z.getName());
else if(z.isDirectory())
{
System.out.println("[" + z.getName() + "]");
SubDirectories(z.listFiles(), lvl + 1);
}
}
}
public static void main(String[] args)
{
String str = "C:\\"; //Enter the directory you want to check the program for
File directories = new File(str);
if(directories.exists() && directories.isDirectory())
{
File fl[] = directories.listFiles();
System.out.println("**********************************************");
System.out.println("Files from main directory : " + directories);
System.out.println("**********************************************");
SubDirectories(fl, 0);
}
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a Java program that lists all the files in a directory and their subdirectories, that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • COSC 3411 /ITAP 3411 Homework (UNIX Shell Commands) How would you ensure that all ordinary files...

    COSC 3411 /ITAP 3411 Homework (UNIX Shell Commands) How would you ensure that all ordinary files created by you have rw-rw---- as default permissions? How would you sort in the background a file called "bad.txt", and place the results in a file called "sort.txt"? Archive the contents of your home directory (including any subdirectories) using tar. Compress the tar archive with gzip. Now extract their contents. Use the “find” command to locate in /docs and /usr/docs all files that Begin...

  • Reading and Writing Files in a Directory Create a program that provides a listing of all...

    Reading and Writing Files in a Directory Create a program that provides a listing of all the files in a directory. The program should be a Java application that accepts a single argument into the args array of the main() method. This argument identifies the name of a directory. The application should make sure that this filename really does identify a directory and then list all of the files in the directory. For each file, list whether the file is...

  • What program do you use to list the files in a directory? Which option do you...

    What program do you use to list the files in a directory? Which option do you use to display: All files, including dotfiles (hidden files) Names in reverse alphabetical order Entire tree, including all subdirectories Flags after each name (/ = directory) Size of each file, in human readable units Long listings (permissions, owner, and so on) Information about a directory itself, not its contents ​When you look at a directory listing, what do the entries . and .. mean?

  • Write the program in Java. Description: Write a program to calculate the total size of all...

    Write the program in Java. Description: Write a program to calculate the total size of all files in the current directory / folder and all sub-folders. Example: I have a directory with 1 file (size 150KB) and 2 subdirectories, each of which has 1 file (size 10KB each), then when I run this program on this directory, I expect the answer to be 170KB.

  • 1. Navigate to your home directory by typing cd. Now type ls and notice that there...

    1. Navigate to your home directory by typing cd. Now type ls and notice that there is a subdirectory called Downloads. Now navigate to the root of the file system by typing cd /. From here, which single command would you use to navigate directly to the Downloads folder that is located in your home directory? The output of the command would be as follows: 2. From the Downloads directory which single command would you use to navigate back up...

  • do numbers 4-8 4. Given any directory, use the Is command to display: • all files...

    do numbers 4-8 4. Given any directory, use the Is command to display: • all files and sub-directories starting with the letter "D" (note do not list anything in any sub-directory) • its immediate sub-directories (sub-directories only, and no other ordinary files) its immediate hidden sub-directories only - take a screenshot (#3-3) that clearly shows the command and the result. 5. Assume that the following files are in the working directory: $ ls intro notesb ref2 section 1 section3 section4b...

  • A filename con comprise multiple embedded dots (e.g., a.b.c.d.e). True False A device file is not...

    A filename con comprise multiple embedded dots (e.g., a.b.c.d.e). True False A device file is not really a stream of characters and doesn't contain anything at all. True False Relative pathnames begin with the root directory. True False Running the ls -a command uniquely identifies directories and binary executables. True False What is the result of running mkdir -p share/man/cat1 from the command line? a. It creates the directory share. b. It creates the directory share/man. c. It creates the...

  • QUESTION 1 What will be the output of following Unix command: find / -name ‘*’ A....

    QUESTION 1 What will be the output of following Unix command: find / -name ‘*’ A. List all files and directories recursively starting from / B. List a file names * in / C. List all files in / directory D. List all files and directories in / directory QUESTION 2 Which command is used to extract a column/field from a text file / input. A. paste B. get C. cut D. tar QUESTION 3 Which command creates an empty...

  • As you know, Is lists all the files in a directory and wc counts words, characters,...

    As you know, Is lists all the files in a directory and wc counts words, characters, and lines. Which of the following operations counts the amount of files (and directories)? Select one: O b. Is wc -w c. Is 1I cat | wc -w In the picture an output of the command Is -I is shown. Which of the following statements is true? druxruxrux. 3 kuivi users 80 May 16 2001 dir ru-ru-rw-. 1 kuivi users 1696 Oct 19 1995...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT