• Register
0 votes
92 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

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 15 views
15 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 LizzyM 6.1k points
0 votes
1 answer 18 views
18 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 LizzyM 6.1k points
1 vote
1 answer 145 views
145 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 Gavin 15.3k points
0 votes
1 answer 11 views
11 views
Problem: Hi! I want to check whether an array is empty or null in Java. Any guidance would be appreciated.
asked Oct 12 Sheeza 2.9k points
0 votes
1 answer 661 views
661 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