Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

FileUtilities



The FileUtilities class contains many utility methods to deals with Files and URLs. The FileUtilities2 class contains also some additional utility methods.

Handling URL paths

Several methods allow to handle URL paths and construct children URLs from parent URLs. For example:

Getting an absolute URL

The FileUtilities.getAbsoluteURL(URL, String) method will return the absolute URL knowing the parent URL and the relative of absolute path.

If the relative Path is already absolute, the parent base URL will not be used, else the resulting URL will be constructed using the base URL and the relativePath.

Getting the absolute path of an URL

Several methods allow to get the absolute path of a relative path from a parent directory or URL, with the appropriate separator for the platform. For example:

If the relative Path is already absolute, the parent base URL will not be used, else the path will be constructed using the base URL and the relativePath.

For example:
   URL parentURL = new URL("file:/L:/WRK/toto/test/resources");
   String path = FileUtilities.getAbsolutePath(parentURL, "myFile.xml");
   // path will be "L:\WRK\toto\test\resources\myFile.xml" on Windows

   URL parentURL = new URL("file:/L:/WRK/my/files");
   String path = FileUtilities.getAbsolutePath(parentURL, "L:/WRK/toto/test/resources/myFile.xml");
   // path will be "L:\WRK\toto\test\resources\myFile.xml" on Windows

Removing the relative delimiters of a path

The FileUtilities.makeAbsoluteURLPath(String) will remove the relative delimiters ( .. and .) from a path and return the associated URL.

For example:
  URL url = FileUtilities.makeAbsoluteURLPath("L:/WRK/Java/resources/MyFile.xml");
  // url will be "file:L:/WRK/Java/resources/MyFile.xml"

  url = FileUtilities.makeAbsoluteURLPath("L:/WRK/Java/resources/../MyFile.xml");
  // url will be "file:L:/WRK/Java/MyFile.xml"

  url = FileUtilities.makeAbsoluteURLPath("L:/WRK/Java/resources/./MyFile.xml");
  // url will be "file:L:/WRK/Java/resources/MyFile.xml"

Detecting absolute paths

The FileUtilities.isAbsolute(String) method will return true if a file path is absolute.

The FileUtilities.isAbsoluteURI(String) method will return true if an URI is absolute.

Handling escape sequences in URL paths

The methods in this class will try to:
  • Convert space characters to their equivalent "%20" escape sequence when handling URL paths
  • Keep "%20" escape sequence characters when handling URL paths
  • Property convert them to space characters when converting to files

Checking file or URL existence

Checking file existence

Two methods allow to check for file existence: The FileUtilities.exist(File, int) method use the following options for the check:

Checking URL existence

Two methods allow to check for file existence: The second method can use the following options for the check: These options can be associated. For example:
   int option = FileUtilities.OPTION_FILEEXIST_SKIP_QUERIES + OPTION_FILEEXIST_SKIP_PHP;
   boolean exists = FileUtilities.exist(theUrl);
Will check if the URL exist, but will consider that the URL does not exist if:
  • The query part of the URL is not null
  • Or the URL points to a php resource

Checking if an URL is found

The FileUtilities.isURLFound(URL, int) method allows to check if it is possible to access an URL. Several options can be combined: It is possible to add a timeOut to check for the URL existance. Also note that special versions of this method allow to use a Proxy and a user agent to get the URL content.

Getting the child of a parent URL

Several methods allow to get a child URL, knowing the parent URL and the relative path. Note that if the path is absolute, then ii will return the URL corresponding with the path.

Getting the child of a parent URL without options

The FileUtilities.getChildURL(URL, String) method will get all a child URL, knowing the parent URL and the relative path.

Note that:
  • The method will not filter URLs corresponding to not existing resources
  • The resulting URL will escape spaces in file paths by a "%20" String

Getting the child of a parent URL with options

The FileUtilities.getChildURL(URL, String, int) method will get all a child URL, knowing the parent URL and the relative path. Several options can be combined:

Getting all the direct children of a parent URL

The FileUtilities.getChildren(URL, boolean) method will get all the direct children URLs for a parent URL, including in the case where the parent URL is a zip or jar file.

Getting the URL of an entry in an archive

The FileUtilities.getJarEntryURL(URL, String) method allow to get the URL of an entry in a Jar or Zip file.

For example, suppose that we have the following zip file:
      zipfile.zip
         - directchild.txt
         - directory
            - file1.txt
To get the URL for the directchild.txt entry, we can do:
   File zipFile = new File("zipfile.zip");
   URL url = zipFile.toURI().toURL();
   URL childURL = FileUtilities.getJarEntryURL(url, "directchild.txt");
To get the URL for the file1.txt entry, we can do:
   File zipFile = new File("zipfile.zip");
   URL url = zipFile.toURI().toURL();
   URL childURL = FileUtilities.getJarEntryURL(url, "directory/file1.txt");

Detecting if a file or URL is an archive

The FileUtilities.isArchive(URL) and FileUtilities.isArchive(File) methods detect if a File is an archive. Note that it does this by reading the first bytes of the File, so it does not rely on the File extension.

See also


Categories: io | packages

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences