• Register
0 votes
813 views

Problem:

I am new at java programming and I used a method:
java.io.File>list(

which is used to obatain the list of files and and directories in the specified directory defined by its path name. This list of files are stored in a string array. If the length of this string array is greater than 0, then the specified directory is not empty. Otherwise, it is empty. A program that demonstrates this is:

File directory = new File

    if (directory.length() > 0) {
            System.out.println("The directory " + directory.getPath() + " is not empty");
         } else {
            System.out.println("The directory " + directory.getPath() + " is empty");
         }
      }

But it does not seems to work can anyone help?

 
10 7 2
6,060 points

Please log in or register to answer this question.

2 Answers

0 votes

Answer:

Checking if a directory is empty or not should not be in one If operator, because you will have mistake in logic. First of all you are checking if it is directory or not, and after that you are checking directory consistensy, becouse you will get wrong answer if the tranfered to method directory is not exist. Just check it. Should be like this:

public static void main(String[] args) {
      File directory = new File("C:\\JavaProgram");
      if (directory.isDirectory()) {
         String[] files = directory.list();
         if (directory.length() > 0) {
            System.out.println("The directory " + directory.getPath() + " is not empty");
         } else {
            System.out.println("The directory " + directory.getPath() + " is empty");
         }
      }
   }

 

11 6 4
34,950 points
0 votes

Solution:

rd /s /q DIRNAME
rmdir /s /q DIRNAME

Close whatever program is occupying them open, possibly your browser, and attempt again.

Let me guess, your attempting to delete your %TMP% folder.

It will delete each file and folder that it can. Hence, in case c:\tmp\dir2\dir3\open.txt is open, c:\tmp\emptyDir is an empty directory, and you do this:

c:\>dir c:\tmp /b /s
c:\tmp\a.txt
c:\tmp\dir2\b.txt
c:\tmp\dir2\dir3\open.txt

c:\>rd /q /s c:\tmp

c:\>dir /s /b c:\tmp
c:\tmp\dir2\dir3\open.txt

You will have deleted:

c:\tmp\a.txt
c:\tmp\dir2\b.txt

And removed:

c:\tmp\emptyDir

However still have the directories.

c:\tmp
c:\tmp\dir2
c:\tmp\dir2\dir3

.an the file:

c:\tmp\dir2\dir3\open.txt

In case instead, a.txt was open, you'd just have:

c:\tmp\

and

c:\tmp\a.txt
rmdir c:\<directory> /s /q

Java Delete Directory

Java File delete() system deletes a file or an empty directory. But, in case directory is not empty, it doesn’t delete it and returns false.

We will employ delete() function recursively to delete a directory/folder in java program.


package com.journaldev.files;

import java.io.File;

/**
 * This utility class can be used to delete 
 * folders recursively in java
 * @author pankaj
 */
public class DeleteFolderRecursively {

    public static void main(String[] args) {
        String folder = "/Users/pankaj/tmp";
        //delete folder recursively
        recursiveDelete(new File(folder));
    }
    
    public static void recursiveDelete(File file) {
        //to end the recursive loop
        if (!file.exists())
            return;
        
        //if directory, go inside and call recursively
        if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                //call recursively
                recursiveDelete(f);
            }
        }
        //call delete to delete files and empty directory
        file.delete();
        System.out.println("Deleted file/folder: "+file.getAbsolutePath());
    }

}

Here we are employing delete() to delete the folder contents and in case it’s directory, we are recursively calling recursiveDelete method.

10 6 4
31,120 points

Related questions

0 votes
1 answer 40 views
40 views
I am new at java programing, I have an assginment about how to find largest number in array and I start coding on my own and i wrote a program given bellow : int size ; int array[] = {10, 20, 25, 63, 96, 57}; size = array.length; for(int i = 0; i<size; i++ ){ ... j]){ size = array[i]; } } System.out.println("Third largest element is:: "+array[size]) } but it is not running, what should I do now?
asked Mar 16, 2020 LizzyM 6.1k points
0 votes
1 answer 22 views
22 views
Problem: How can I declare an array in Java?
asked Feb 12 sasha 16.2k points
0 votes
1 answer 16 views
16 views
Illustrate with an example
asked Nov 16, 2020 jagruthi 680 points
0 votes
1 answer 5 views
5 views
PROBLEM : Please help me on > 'how to remove element from array in java with example' ?
asked Mar 16 Samiur1 38.4k points
0 votes
1 answer 10 views
10 views
Problem How can i remove an element from array in c++
asked Mar 25 Subhendu 4.5k points
0 votes
1 answer 18 views
18 views
Problem: C ++ program that reads a flat file (mycode.txt) with the sequence ATCGGATCGCCAATGCGGATGCTTTATAATCCGTA # ( It is a file created in notepad with the name mycode.txt and is located in the folder where my compiler saves the cpp and exe ) The program must store the sequence in a char type array ... +) arr [i] = l.toCharArray (); } } int main () { file sequence; file.Open (); return 0; } `` ''
asked Nov 5, 2020 sasha 16.2k points
0 votes
1 answer 27 views
27 views
Problem: I tried to run java in my system but it shows : Java is not recognized as an internal or external command, Java is not internal or external command , Java command not working. I will get this type of errors when I do not configure java environment variables. I have ... thing..but I don't no what to do next anyone hlep me about me i can i configure it after installing it. Can anyone help?
asked Mar 21, 2020 LizzyM 6.1k points
0 votes
1 answer 2 views
2 views
Problem: Can you tell me what is the length of empty string?
asked Apr 13 jamuna1 30.2k points
0 votes
1 answer 7 views
7 views
Problem: anyone who know this problem please help? how to check if a string is empty in java
asked Mar 22 Rohit kr 17.5k points
1 vote
1 answer 5 views
5 views
Problem : Anyone know this > Which is the correct way to trim a string in Java?
asked 6 days ago Abik Dey 4k points