FileSystem

public class FileSystem

FileSystem encapsulates a root directory and provides accessors for querying and modifying this root.

Author:Hugo Y. K. Lam

Fields

TYPE_ALL

public static int TYPE_ALL

Type All.

TYPE_DIR

public static int TYPE_DIR

Type Directory.

TYPE_FILE

public static int TYPE_FILE

Type File.

Constructors

FileSystem

public FileSystem(File root)

Creates a new instance of FileSystem.

Parameters:
  • root – the root of this file system. If root is null, it will be set to the current user directory.

Methods

exists

public boolean exists()

Checks if the root of this file system exists.

Returns:true if the root of this file system exists.

getDirectories

public Collection getDirectories(boolean isRecursive)

Retrieves a collection of directory-only File objects from the root of this file system.

Parameters:
  • isRecursive – true if the search should be recursive.
Returns:

a collection of File objects resulted from the search.

getDirectories

public Collection getDirectories(boolean isRecursive, String pattern)

Retrieves a collection of directory-only File objects from the root of this file system.

Parameters:
  • isRecursive – true if the search should be recursive.
  • pattern – the filename’s pattern for filtering the result. null if no filtering should be applied.
Returns:

a collection of File objects resulted from the search.

getFiles

public Collection getFiles(boolean isRecursive)

Retrieves a collection of file-only File objects from the root of this file system.

Parameters:
  • isRecursive – true if the search should be recursive.
Returns:

a collection of File objects resulted from the search.

getFiles

public Collection getFiles(boolean isRecursive, String pattern)

Retrieves a collection of file-only File objects from the root of this file system.

Parameters:
  • isRecursive – true if the search should be recursive.
  • pattern – the filename’s pattern for filtering the result. null if no filtering should be applied.
Returns:

a collection of File objects resulted from the search.

getFiles

public Collection getFiles(boolean isRecursive, int type, String pattern)

Retrieves a collection of File objects from the root of this file system.

Parameters:
  • isRecursive – true if the search should be recursive.
  • type – the file type to be searched.
  • pattern – the filename’s pattern for filtering the result. null if no filtering should be applied.
Returns:

a collection of File objects resulted from the search.

getFiles

public Collection getFiles(Collection c, boolean isRecursive, int type, String pattern)

Retrieves a collection of File objects from the root of this file system.

Parameters:
  • c – the collection into which the result will be stored.
  • isRecursive – true if the search should be recursive.
  • type – the file type to be searched.
  • pattern – the filename’s pattern for filtering the result. null if no filtering should be applied.
Returns:

a collection of File objects resulted from the search.

getRoot

public File getRoot()

Gets the root of this file system.

Returns:the root of this file system.

purge

public void purge()

Removes the files, including the directories, under the root directory of this file system recursively. If there are files that cannot be removed immediately, the files will be deleted on exit. This method will also remove the root directory itself.

purge

public void purge(boolean isSelfRemoved)

Removes the files, including the directories, under the root directory of this file system recursively. If there are files that cannot be removed immediately, the files will be deleted on exit.

Parameters:
  • isSelfRemoved – true if the root directory itself should be removed.

remove

public boolean remove()

Removes the files, including the directories, under the root directory of this file system recursively. The operation ceases when it encounters any error in removing any file. If the operation is successful, the root directory itself will be removed as well.

Returns:true if and only if all the files are removed successfully.

remove

public boolean remove(boolean isSelfRemoved)

Removes the files, including the directories, under the root of this file system recursively. The operation ceases when it encounters any error in removing any file.

Parameters:
  • isSelfRemoved – true if the root directory itself should be removed.
Returns:

true if and only if all the files are removed successfully.

toString

public String toString()

Returns the absolute path of the root of this file system.

Returns:the absolute path of the root of this file system.

See also: java.lang.Object.toString()