java.io 
Class File
---------------------------------------
isDirectory
public boolean isDirectory()
Tests whether the file denoted by this abstract pathname is a directory.
Returns:
true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise
Throws:
SecurityException - If a security manager exists and its SecurityManager.checkRead(java.io.FileDescriptor) method denies read access to the file
-------------------------------------------------
mkdirs
public boolean mkdirs()
Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.
Returns:
true if and only if the directory was created, along with all necessary parent directories; false otherwise
Throws:
SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.io.FileDescriptor) method does not permit the named directory and all necessary parent directories and to be created
------------------------------------------------------------------
listFiles
public File[] listFiles()
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. 
If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned, one for each file or directory in the directory. Pathnames denoting the directory itself and the directory's parent directory are not included in the result. Each resulting abstract pathname is constructed from this abstract pathname using the File(File, String) constructor. Therefore if this pathname is absolute then each resulting pathname is absolute; if this pathname is relative then each resulting pathname will be relative to the same directory. There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.Returns:
An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
Throws:
SecurityException - If a security manager exists and its SecurityManager.checkRead(java.io.FileDescriptor) method denies read access to the directory
Since: 
JDK1.2 
这个里面,如果返回为空的话,就代表空目录。

解决方案 »

  1.   

    if (file.list()==0)
    {
    //为空}具体方法使用在API上写的很清楚啊。list()返回目录内的文件名及子目录名。
      

  2.   

    import java.io.*;class FileTest{
      public static void main(String args[]){
        System.out.println("path separator "+File.pathSeparator);
        System.out.println("path separator char"+File.pathSeparatorChar);
        System.out.println("separator "+File.separator);
        System.out.println("separator char "+File.separatorChar);
        File f=new File("readme.txt");
     System.out.println();
     System.out.println(f);
     System.out.println("exist? "+f.exists());
     System.out.println("name "+f.getName());
     System.out.println("path "+f.getPath());
     System.out.println("absolute path "+f.getAbsolutePath());
    System.out.println("Is a file? "+f.isFile());
      System.out.println("Is a directory? "+f.isDirectory());
      System.out.println("can read? "+f.canRead());
     System.out.println("can write? "+f.canWrite());
      System.out.println("last modified "+f.lastModified());
        File newF=new File("NewFile");
        System.out.println("¡­Rename "+f+"...");
     f.renameTo(newF);
       System.out.println("name "+newF.getName());
     System.out.println(f+" exist?  "+f.exists());
     System.out.println("¡­delete " +newF+"...");
     newF.delete();
       System.out.println(newF+"exist? " +newF.exists());
      }
    }
      

  3.   

    public class TestFile
    {
        public static void main(String[] args) throws Exception
        {
            java.io.File file = new java.io.File("e:/log");
            String[] list = file.list();
            for(int i=0;i<list.length;i++)
                System.out.println(list[i]);
            if (list.length==0)
                System.out.println("目录为空");
        }}
      

  4.   

    File f1=new File("D:/test");
          if(f1.exists() && f1.isDirectory()){//存在且是目录}
      

  5.   

    存在:file.exists()
    为空目录:file.list().length==0