• Register
0 votes
377 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 26 views
26 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 11 views
11 views
Illustrate with an example
asked Nov 16, 2020 jagruthi 680 points
0 votes
1 answer 20 views
20 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 18 views
18 views
Is Anyone face the same issue?
asked Dec 27, 2020 TeamScript 9.7k points
1 vote
1 answer 268 views
268 views
Problem: Hello Guys, I am trying to import a Maven project in my Eclipse IDE but the project is not importing properly. It shows an error, source folder is not a java project when I m trying to give it a new call-in it. The problem is literally going over my head ... the problem, as well as have a solution, please speak up. I'm surfing over the internet for an hour but couldn&rsquo;t fix this yet.
asked Apr 2, 2020 Gavin 15.3k points
0 votes
1 answer 21 views
21 views
Problem: Hi! I want to check whether an array is empty or null in Java. Any guidance would be appreciated.
asked Oct 12, 2020 Code Learner 5.5k points
0 votes
1 answer 8 views
8 views
Problem: The error ValueError: could not convert string to float: is caused by a parsing error in the function float () with a string argument which can not be parsed as a float number. The float () function is used to convert a string with a float value to a float value. The float function parses the string and converts it as a float number.
asked Dec 2, 2020 Mashhoodch 5.7k points
0 votes
1 answer 7 views
7 views
Problem: One of the main features of the Java language keeps you in check so that your programs work in the way that you intend. You may encounter an error saying &ldquo;not all arguments converted during string formatting&rdquo; How i working with strings.
asked Dec 2, 2020 Mashhoodch 5.7k points
0 votes
1 answer 872 views
872 views
Problem : Getting bellow error related to curses in python code curses is not supported on this machine (please install/reinstall curses for an optimal experience)
asked Nov 7, 2019 peterlaw 6.9k points
0 votes
1 answer 13 views
13 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 5.3k points