Question

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 a file or a directory and list the size for each file. The program should print its output to System.out.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Here is the completed code for this problem.  Let me know if you have any doubts or if you need anything to change.

Thank You !!

================================================================================================

public class DirectoryListing {

    public static void main(String[] args) {

        if(args.length==0){
            System.out.println("Please enter the directory as command line argument.");
            System.exit(0);
        }
        String directoryPath=args[0];
        File folder = new File(directoryPath);
        if(folder.isDirectory()){

            File[] listOfFiles = folder.listFiles();
            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                    System.out.println("File : " + listOfFiles[i].getName()+" File Size: "+listOfFiles[i].length()+" bytes");
                } else if (listOfFiles[i].isDirectory()) {
                    System.out.println("Directory : " + listOfFiles[i].getName());
                }
            }

        }else{
            System.out.println(directoryPath+" is not a directory");
        }

    }
}

======================================================================================

Add a comment
Know the answer?
Add Answer to:
Reading and Writing Files in a Directory Create a program that provides a listing of all...
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
  • How do I correct this error in my code? When it got to the file type...

    How do I correct this error in my code? When it got to the file type I can only go "null" from a certain point and I'm wondering how to correct this error. Also can I get a word document on this coding strategy for this problem? How much of this am I doing correctly? The original problem description: Create a program that provides a listing of all the files in a directory. The program should be a Java application...

  • 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...

  • JAVA: create a program that reads all text files in a directory, calculates the occurrences of...

    JAVA: create a program that reads all text files in a directory, calculates the occurrences of words, and saves the result in one single output text file. assume that you are in a directory called Desktop/programdirectory, where you have multiple text files. textfile1.txt : "how are you" textfile2.txt: "great thank you" textfile3.txt: "great to see you. see you later" Then, your output.txt should have: how: 1 are: 1 you: 4 great: 2 thank: 1 to: 1 see: 2 later: 1

  • Write a program to count a number of files for each extension in the given directory....

    Write a program to count a number of files for each extension in the given directory. The program should take a directory name as argument and print count and extension for each available file extension. For example, the output would be something like this: python counter.py \yourpathhere 14 py 4 txt 1 csv

  • You will be reading in 3 files in the program. One will contain a list of...

    You will be reading in 3 files in the program. One will contain a list of 1000 words in unsorted order. The second file will contain 1000 words in sorted order. The final file will contain 20 words to be searched for. The main program has been written for you. You will be implementing three functions: bool readWords(string array[], int size, string fileName); int linearSearch(string wordToFind, const string words[], int size); int binarySearch(string wordToFind, const string words[], int size); The...

  • 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?

  • I have one method {search()} that search for a particular file in a directory that the filename s...

    I have one method {search()} that search for a particular file in a directory that the filename start by "B" and it will return the files. I have 2 file that start by B [Bcc.txt, Ba.txt]. But in main method I want to count a word "light" how many times it appeares in the each files [Bcc.txt, Ba.txt]. However, I really don't know how to do it. I need help on that. public class CountWord {    public static void main(String[]...

  • Assume access control list of three files are given as below in Unix directory listing. (you...

    Assume access control list of three files are given as below in Unix directory listing. (you will see something similar when you type ls -l in a unix system)   Access Control FileName Group Owner Drw-rwxr-x Classess University Nick drwxr----- bills University Kate -rwxrw-rw- list OS_Class Sandra -rwxr-x--x Assignment2 OS_Class Nancy Narrate the permission given to the owner, group and everybody for each file or folder. Hint: d- directory (if it is a folder, otherwise -) r- read permission w- write...

  • Using only shell commands, create a directory structure in your Linux file system to organize files...

    Using only shell commands, create a directory structure in your Linux file system to organize files for this course. An example structure may look like the following: osweb homework homework1.c homework2.java homework3.cpp labs lab1 setup.txt projects Include the following by only using shell commands: List the contents of a directory Write data to a text file Print the contents of a text file to the terminal Delete a text file Move a text file to a different directory Rename a...

  • Reading and Writing Complete Files in C: The first part of the lab is to write...

    Reading and Writing Complete Files in C: The first part of the lab is to write a program to read the complete contents of a file to a string. This code will be used in subsequent coding problems. You will need 3 functions: main(), read_file() and write_file(). The main function contains the driver code. The read_file() function reads the complete contents of a file to a string. The write_file() writes the complete contents of a string to a file. The...

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