Checks, whether the child directory is a subdirectory of the base directory. : Directory « File « Java Tutorial






import java.io.File;
import java.io.IOException;

/* 
 * JCommon : a free general purpose class library for the Java(tm) platform
 * 
 *
 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
 * 
 * Project Info:  http://www.jfree.org/jcommon/index.html
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA.  
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 * 
 * ------------
 * IOUtils.java
 * ------------
 * (C)opyright 2002-2004, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   David Gilbert (for Object Refinery Limited);
 *
 * $Id: IOUtils.java,v 1.8 2009/01/22 08:34:58 taqua Exp $
 *
 * Changes
 * -------
 * 26-Jan-2003 : Initial version
 * 23-Feb-2003 : Documentation
 * 25-Feb-2003 : Fixed Checkstyle issues (DG);
 * 29-Apr-2003 : Moved to jcommon
 * 04-Jan-2004 : Fixed JDK 1.2.2 issues with createRelativeURL;
 *               added support for query strings within these urls (TM);
 */


/**
 * The IOUtils provide some IO related helper methods.
 *
 * @author Thomas Morgner.
 */
public class Main {
  /**
   * Checks, whether the child directory is a subdirectory of the base 
   * directory.
   *
   * @param base the base directory.
   * @param child the suspected child directory.
   * @return true, if the child is a subdirectory of the base directory.
   * @throws IOException if an IOError occured during the test.
   */
  public boolean isSubDirectory(File base, File child)
      throws IOException {
      base = base.getCanonicalFile();
      child = child.getCanonicalFile();

      File parentFile = child;
      while (parentFile != null) {
          if (base.equals(parentFile)) {
              return true;
          }
          parentFile = parentFile.getParentFile();
      }
      return false;
  }
}








11.4.Directory
11.4.1.Create a directory (or several directories)
11.4.2.Create directory
11.4.3.Create directory along with required nonexistent parent directories
11.4.4.Create a directory; all ancestor directories must exist
11.4.5.Create a directory; all non-existent ancestor directories are automatically created
11.4.6.Create a directories recursively
11.4.7.Creates a new and empty directory in the default temp directory using the given prefix.
11.4.8.Determine File or Directory
11.4.9.Deleting a Directory (an empty directory)
11.4.10.Determine if file or directory exists
11.4.11.Determine if File or Directory is hidden
11.4.12.If a file or directory readable
11.4.13.Get name of parent directory
11.4.14.Get name of specified file or directory
11.4.15.Get parent directory as a File object
11.4.16.Get the content of a directory
11.4.17.Checks, whether the child directory is a subdirectory of the base directory.
11.4.18.Delete file or directory
11.4.19.Delete directory recursively
11.4.20.Delete file or directory when virtual machine terminates
11.4.21.Calculate directory size
11.4.22.Change last modified time of a file or directory
11.4.23.Mark file or directory Read Only
11.4.24.Rename file or directory
11.4.25.Moving a File or Directory to Another Directory
11.4.26.Search for files recursively
11.4.27.Traversing all files and directories under dir
11.4.28.Traversing only directories under dir
11.4.29.Traversing only files under dir
11.4.30.Copying a Directory
11.4.31.Check if a directory is not empty
11.4.32.Getting the Current Working Directory
11.4.33.Get all files and folders under a certain folder and save them to a set
11.4.34.Recursively search a directory tree
11.4.35.Searches through the directory tree