com.sas.io
Class File

com.sas.io.File
All Implemented Interfaces:
com.sas.beans.PropertyChangeSource, com.sas.beans.VetoableChangeSource, com.sas.collection.ContentsChangedSource, StaticCollectionInterface, StaticOrderedCollectionInterface, ComponentInterface, LinkPropertiesInterface, ModelInterface, NodeIconInterface, NodeInterface, SimpleNodeInterface, com.sas.PublicClonable, com.sas.util.Countable, com.sas.util.Enumerable, IndexedGetInterface, ViewInterface, MultipleValueEventSourceInterface, java.beans.PropertyChangeListener, java.io.ObjectInputValidation, java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<java.io.File>, java.util.EventListener

public class File
implements StaticOrderedCollectionInterface, NodeIconInterface

A class representing a File on the host file system. This class is designed for applications and may not work with applets because of security issues.

Creating a File Component

File a = new File("d:\\images"); File f = new File("d:\\images", "attributes.gif"); File f = new File(a, "attributes.gif");


 

See Also:
Serialized Form

Field Summary
protected  OrderedCollection children
           
protected static com.sas.visuals.IconInterface DRIVE_ICON
           
protected static com.sas.visuals.IconInterface FILE_ICON
           
protected static com.sas.visuals.IconInterface FOLDER_ICON
           
protected static com.sas.visuals.IconInterface FOLDER_OPEN_ICON
           
protected  boolean need_refresh
           
protected  java.lang.String nodeExpandedText
          full path of this file
protected  java.lang.String nodeText
          Name of this file object
protected  java.lang.String nodeType
          Type of file object, file or directory
static java.lang.String RB_KEY
           
 
Constructor Summary
File()
          Default constructor for design time.
File(File directory, java.lang.String name)
          Creates a new File object given a file object and the name of the File.
File(FileSystem fs, File dir, java.lang.String name)
          Creates a File on the specified file system such as UNIX or Windows and specified directory.
File(java.lang.String path)
          Creates a new File object.
File(java.lang.String path, java.lang.String file)
          Creates a new File object.
 
Method Summary
protected  void _setNodeType()
          Sets the type of the File object to Directory or File.
 void addContentsChangedListener(com.sas.collection.ContentsChangedListener listener)
          Add a listener for the com.sas.collection.ContentsChangedEvent event.
 void apply(com.sas.util.ApplyInterface action)
          Applies the 'apply' function in the action to every item in the collection.
 boolean chmod(int flag)
          Method to change the read write permission of a file or directory, so can implement force option for delete.
 java.lang.Object clone()
          Create a copy of the current model
 boolean contains(java.lang.Object element)
          Test for the presence of an object in the collection
 boolean copyTo(File dir)
          Copies the entire Directory and its contents to the specified Directory.
 boolean copyTo(java.lang.String path)
          Copies the entire directory and its contents to the name specified in path.
 int count()
          Gets the number of items in the Directory.
 int countNodeChildren(int maximumChildCount)
          Counts the children of the current node.
 boolean delete(java.lang.String flag)
          Deletes the current directory or file.
static boolean delete(java.lang.String path, java.lang.String flag)
          Deletes a File specified by the path.
 boolean equals(StaticOrderedCollectionInterface orderedCollection)
          Compare the current items with those in another collection and tell whether the collections are identical.
 void fireContentsChanged(com.sas.collection.ContentsChangedEvent event)
          Send a ContentsChangedEvent event to all the ChangedListener objects in the listener list.
 java.lang.Object get(int index)
          Fetch an item at a index.
 com.sas.visuals.IconInterface getDefaultIcon()
          Returns the default icon for the current node.
 com.sas.visuals.IconInterface getExpandedIcon()
          Returns the expanded icon for the current node.
 com.sas.visuals.IconInterface getExpandedSelectedIcon()
          Sets the expandedSelected icon for the current node.
static com.sas.beans.ExtendedBeanInfo getExtendedBeanInfo()
           
 int getIndex(java.lang.Object element, int startIndex)
          Returns the index asschildreniated with the item (searches forward)
 java.util.Enumeration getItems()
          Returns enumeration of File objects from a list.
 java.util.Enumeration getItems(int start, int end)
          Returns an enumeration of a subset of the items from a collection.
 int getLastIndex(java.lang.Object element, int startIndex)
          Returns the index associated with the item (searches backward)
 SimpleNodeInterface getNodeChild(int index)
          Returns the child node at the specified index.
 int getNodeChildCount()
          Returns the number of files and directories.
 SimpleNodeInterface[] getNodeChildren(int startIndex, int count)
          Returns the children of the current node in the specified range.
 java.lang.String getNodeExpandedText()
          If the expanded text is not set then gets an absolute path name of this object.
 java.lang.String getNodeText()
          Gets the Name of this File object.
 java.lang.String getNodeType()
          Gets a String indicating whether this File object is a File or Directory.
 File getParentDir()
          Gets the Parent File object for this File.
 com.sas.visuals.IconInterface getSelectedIcon()
          Returns the selected icon for the current node.
protected  void initializeChildren()
           
 boolean isLeafNode()
          Returns true if this object does not have children and thus is not expandable.
 boolean mkdir(java.lang.String subdirectoryName)
          Creates a subdirectory of this directory object.
 boolean mkdirs(java.lang.String subdirectoryPath)
          Creates all the subdirectories listed in the path. returns false if the directories exists.
 boolean moveTo(java.io.File parentdir)
          Moves the entire directory and its contents to the new parent directory.
 boolean moveTo(java.lang.String parentpath)
          Moves the entire directory and its contents to the parent name specified by path.
 void removeContentsChangedListener(com.sas.collection.ContentsChangedListener listener)
          Remove a listener for the com.sas.collection.ContentsChangedEvent event.
 boolean renameTo(java.lang.String newName)
          /** Changes the name of the File specified by the newName.
 void setDefaultValues()
          Sets the default values
 void setHasChildren(boolean hasChildren)
          Sets the leaf node property to the valuse passed in.
protected  void setNodeText(java.lang.String name)
          Sets the Text of this File object to the absolute path.
 void sort(com.sas.util.Comparator comparator)
          Sorts the items from the current collection using the provided comparison function.
 void sort(com.sas.util.Comparator comparator, int start, int end)
          Sorts a range of items from the current collection using the provided comparison function.
 void supressChildren(boolean hasChildren)
          Sets the leaf node property to the valuse passed in.
 
Methods inherited from class com.sas.io.FileComponent
addLink, addPropertyChangeListener, addVetoableChangeListener, anyPropertyChangeListeners, attachModel, attachView, detachModel, detachView, dumpComponent, firePropertyChange, firePropertyChange, fireVetoableChange, getComponentDescription, getComponentSupportInfo, getEventMethod, getEventValues, getLinkInfo, getModelInterface, getRequiredInterfaces, getViewInterfaceSupportInfo, initialize, initializeComponent, isDesignTime, isLinked, propertyChange, queryLinks, queryLinks, refresh, removeAllLinks, removeInterfaceTraps, removeLink, removePropertyChangeListener, removeVetoableChangeListener, setComponentDescription, setComponentSupportInfo, setLinkInfo, setModelInterface, setRequiredInterfaces, setViewInterfaceSupportInfo, supportsListenerInterface, supportsRequiredInterfaces, trapInterfaceEvents, validateObject
 
Methods inherited from interface com.sas.beans.PropertyChangeSource
addPropertyChangeListener, removePropertyChangeListener
 

Field Detail

children

protected OrderedCollection children

nodeText

protected java.lang.String nodeText
Name of this file object


nodeType

protected java.lang.String nodeType
Type of file object, file or directory


nodeExpandedText

protected java.lang.String nodeExpandedText
full path of this file


need_refresh

protected transient boolean need_refresh

FOLDER_ICON

protected static com.sas.visuals.IconInterface FOLDER_ICON

FILE_ICON

protected static com.sas.visuals.IconInterface FILE_ICON

DRIVE_ICON

protected static com.sas.visuals.IconInterface DRIVE_ICON

FOLDER_OPEN_ICON

protected static com.sas.visuals.IconInterface FOLDER_OPEN_ICON

RB_KEY

public static final java.lang.String RB_KEY
See Also:
Constant Field Values
Constructor Detail

File

public File()
Default constructor for design time.


File

public File(File directory,
            java.lang.String name)
Creates a new File object given a file object and the name of the File.

Parameters:
directory - parent File
name - name of the file

File

public File(FileSystem fs,
            File dir,
            java.lang.String name)
Creates a File on the specified file system such as UNIX or Windows and specified directory.

Parameters:
FileSystem - object
dir - parent File,
name - of the File

File

public File(java.lang.String path)
Creates a new File object. The File cannot be manipulated if the physical file does not exist.

Parameters:
path - absolute path. System dependent File separator character is used in path specification.

File

public File(java.lang.String path,
            java.lang.String file)
Creates a new File object. The File cannot be manipulated if the physical file does not exist.

Parameters:
path - absolute path. System dependent File separator character is used in path specification.
file - name of the file
Method Detail

getExtendedBeanInfo

public static com.sas.beans.ExtendedBeanInfo getExtendedBeanInfo()

setDefaultValues

public void setDefaultValues()
Sets the default values

Specified by:
setDefaultValues in interface ComponentInterface
Overrides:
setDefaultValues in class FileComponent
See Also:
ComponentInterface.setDefaultValues()

delete

public static boolean delete(java.lang.String path,
                             java.lang.String flag)
Deletes a File specified by the path. Deletes the directory or file based on the flag specified. eg. flag is "r f" or "r" or "F"

Parameters:
path - a string specifying the absolute path.
flag - a string specifying the options recursive or force or both
Returns:
True if successful, false otherwise.

addContentsChangedListener

public void addContentsChangedListener(com.sas.collection.ContentsChangedListener listener)
Add a listener for the com.sas.collection.ContentsChangedEvent event.

Specified by:
addContentsChangedListener in interface com.sas.collection.ContentsChangedSource
Parameters:
listener - an object which handles ChangedEvent events the listener is not added a second time if it already exists in the list of listeners for this event.
See Also:
ContentsChangedSource

removeContentsChangedListener

public void removeContentsChangedListener(com.sas.collection.ContentsChangedListener listener)
Remove a listener for the com.sas.collection.ContentsChangedEvent event. Nothing happens if the listener is not in the list of listeners for this event.

Specified by:
removeContentsChangedListener in interface com.sas.collection.ContentsChangedSource
Parameters:
listener - an object which handles ContentsChangedEvent events
See Also:
ContentsChangedSource

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Create a copy of the current model

Specified by:
clone in interface StaticCollectionInterface
Specified by:
clone in interface StaticOrderedCollectionInterface
Specified by:
clone in interface com.sas.PublicClonable
Overrides:
clone in class FileComponent
Returns:
a clone of the object.
Throws:
java.lang.CloneNotSupportedException - if the model attached to the component being cloned will not allow the clone to attach.

fireContentsChanged

public void fireContentsChanged(com.sas.collection.ContentsChangedEvent event)
Send a ContentsChangedEvent event to all the ChangedListener objects in the listener list. The listener list is copied first, so the event is sent only to each item which is in the list at the time of this copy, even if adds or removes are performed after the send has started.

Parameters:
event - the event to send

copyTo

public boolean copyTo(File dir)
Copies the entire Directory and its contents to the specified Directory. The contents of this Directory are copied to the new Directory and subdirectories are copied recursively. e.g. a.copyTo(b) will copy
 a  = d:\apps\af             b = d:\backup

   d:\apps\af\f1               d:\backup\f1
   d:\apps\af\d\s1             d:\backup\d\s1
   d:\apps\af\d\s2             d:\backup\d\s2
   d:\apps\af\d                d:\backup\d
 
If the physical dir does not exist then creates a new directory and copy it.

Parameters:
dir - the new parent directory.
Returns:
True if successful, false otherwise.

copyTo

public boolean copyTo(java.lang.String path)
Copies the entire directory and its contents to the name specified in path.

Parameters:
path - the path of the directory.
Returns:
True if successful, false otherwise.

count

public int count()
Gets the number of items in the Directory.

Specified by:
count in interface com.sas.util.Countable
Returns:
Total number of items in the Directory specified by this object -1 if cannot access directory.

delete

public boolean delete(java.lang.String flag)
Deletes the current directory or file. Deletes the current directory based on the flag specified. The argument could be a string specifying r(recursive) or f(force) or r,f. The order and case does not matter. eg. delete("r,f") or delete("r F") or delete(R F)

Parameters:
flag - specifying whether to delete the directory recursively and force the delete or not. If the current File is not a direcotry but a file then will ignore the recursive flag and delete the file.
Returns:
True if successful, false otherwise.

chmod

public boolean chmod(int flag)
Method to change the read write permission of a file or directory, so can implement force option for delete.

Parameters:
1 - - READ , 2 - WRITE, 3 - READ | WRITE
Returns:
true if succesful, false otherwise

getNodeChildren

public SimpleNodeInterface[] getNodeChildren(int startIndex,
                                             int count)
Returns the children of the current node in the specified range. SimpleNodeInterface implementation

Specified by:
getNodeChildren in interface SimpleNodeInterface
Parameters:
startIndex - index which identifies the start of the range
count - number of children to return
Returns:
an array containing the specified range of children or null if the leafNode property value is true.

getNodeChild

public SimpleNodeInterface getNodeChild(int index)
Returns the child node at the specified index. SimpleNodeInterface implementation

Specified by:
getNodeChild in interface SimpleNodeInterface
Parameters:
index - the index of the child node to retrieve
Returns:
the child at index or null if the leafNode property value is true.

getNodeChildCount

public int getNodeChildCount()
Returns the number of files and directories.

Specified by:
getNodeChildCount in interface SimpleNodeInterface
Returns:
number of files and directories in this directories.

countNodeChildren

public int countNodeChildren(int maximumChildCount)
Counts the children of the current node. The result of getNodeChildCount() is returned from this method, ignoring maximumChildCount.

Specified by:
countNodeChildren in interface SimpleNodeInterface
Parameters:
maximumChildCount - the maximum number of children to read, or 0 to read indefinitely
Returns:
The number of children known to be available.

getNodeExpandedText

public java.lang.String getNodeExpandedText()
If the expanded text is not set then gets an absolute path name of this object. SimpleNodeInterface implementation

Specified by:
getNodeExpandedText in interface NodeInterface
Returns:
String absolute path.

isLeafNode

public boolean isLeafNode()
Returns true if this object does not have children and thus is not expandable. SimpleNodeInterface implementation

Specified by:
isLeafNode in interface SimpleNodeInterface
Returns:
boolean True if has no children, False otherwise.

getNodeText

public java.lang.String getNodeText()
Gets the Name of this File object. TreeInterface implementation

Specified by:
getNodeText in interface NodeInterface
Returns:
String name of this File.

getNodeType

public java.lang.String getNodeType()
Gets a String indicating whether this File object is a File or Directory. NodeInterface implementation

Specified by:
getNodeType in interface NodeInterface
Returns:
String File type.

getItems

public java.util.Enumeration getItems()
Returns enumeration of File objects from a list.

Specified by:
getItems in interface com.sas.util.Enumerable
Returns:
OrderedCollection of File objects.

initializeChildren

protected void initializeChildren()

getParentDir

public File getParentDir()
Gets the Parent File object for this File.

Returns:
com.sas.io.File parent directory or null if this is a root directory

mkdir

public boolean mkdir(java.lang.String subdirectoryName)
              throws java.io.FileNotFoundException
Creates a subdirectory of this directory object. Returns false if the directory already exists.
 eg. File f = new File("d:\\app");
 boolean rc = f.exists();
 boolean didMake = f.mkdir("test1"); 
    - Will create a directory "d:\\app\\test1" only if rc == true otherwise
      throws FileNotFoundException.
    - If invalid argument(absolutePath or invalid separator character) 
      specified then didMake is false
 didMake = f.mkdir();
   - This is inherited method which will create a directory only if
     rc == false and didMake = true
 

Parameters:
subdirectoryName - name of the subdirectory
Returns:
true if successful, false otherwise.
Throws:
java.io.FileNotFoundException - if this(parent) directory does not exists.

mkdirs

public boolean mkdirs(java.lang.String subdirectoryPath)
               throws java.io.FileNotFoundException
Creates all the subdirectories listed in the path. returns false if the directories exists.
 eg. File f = new File("d:\\app\\backup");
 boolean rc = f.exists();
 boolean didMake = f.mkdirs("a\\b\\c"); 
    - Will create all the directories "d:\\app\\backup\\a\\b\\c" only if rc == true 
      otherwise throws FileNotFoundException.
    - If invalid argument(absolutePath or invalid separator character) 
      specified then didMake is false
 didMake = f.mkdirs();
   - This is inherited method which will create all directories only if
     rc == false and returns didMake = true
 

Parameters:
subdirectorypath - path of the subdirectory.
Returns:
true if successful, false otherwise.
Throws:
java.io.FileNotFoundException - if this(parent) directory does not exists.

moveTo

public boolean moveTo(java.io.File parentdir)
Moves the entire directory and its contents to the new parent directory.
 eg. File newf = new File("c:\\test\\backup");
 File f = new File("c:\\f1");
 boolean rc = f.moveTo(newf);
    - will move the file f1 from its parent directory c:\\ to
      c:\\test\\backup
    - will return false if the new parent is not a directory or do not 
      have access to the file or directory.  
    - if f1 is a directory will move the entire directory and its contents.
 This is similar to copyTo(dir), except that if this object is a directory
 then copyTo will remove that directory and copy the contents to 
 the new directory. Whereas moveTo will append new to the parent directory.
 

Parameters:
parentdir - the new parent directory.
Returns:
boolean true if successful, false otherwise.

moveTo

public boolean moveTo(java.lang.String parentpath)
Moves the entire directory and its contents to the parent name specified by path. Does not move if the name does not exists.

Parameters:
path - the directory.
Returns:
True if successful, false otherwise.

renameTo

public boolean renameTo(java.lang.String newName)
/** Changes the name of the File specified by the newName.
 File f = new File("d:\\apps\\a");
 if absolute path specified as an argument then
 f.renameTo("d:\\test");  // will rename "d:\\apps\\a" to "d:\\test"
 if only name is specified then
 f.renameTo("test"); // will rename "d:\\apps\\a" to "d:\\apps\\test"
 

Parameters:
newName - path of new File. If not absolute will change the name of the file.
Returns:
True if successful, false otherwise.

_setNodeType

protected void _setNodeType()
Sets the type of the File object to Directory or File.


setNodeText

protected void setNodeText(java.lang.String name)
Sets the Text of this File object to the absolute path.


setHasChildren

public void setHasChildren(boolean hasChildren)
Sets the leaf node property to the valuse passed in.

Parameters:
hasChildren - boolean flag to set the leafNode property

supressChildren

public void supressChildren(boolean hasChildren)
Sets the leaf node property to the valuse passed in.

Parameters:
hasChildren - boolean flag to set the leafNode property

equals

public boolean equals(StaticOrderedCollectionInterface orderedCollection)
Compare the current items with those in another collection and tell whether the collections are identical. The equals method is used to compare collection items.

Specified by:
equals in interface StaticOrderedCollectionInterface
Parameters:
orderedCollection - Another ordered collection to be compared to.
Returns:
True if identical, False otherwise.

get

public java.lang.Object get(int index)
Fetch an item at a index.

Specified by:
get in interface IndexedGetInterface
Parameters:
index - the position of the item, zero-based indexing.
Returns:
the item asschildreniated with the index value
Throws:
java.lang.IndexOutOfBoundsException - if index is not in the range [0, count()-1].

getIndex

public int getIndex(java.lang.Object element,
                    int startIndex)
Returns the index asschildreniated with the item (searches forward)

Specified by:
getIndex in interface StaticOrderedCollectionInterface
Parameters:
element - item to search for
startIndex - the position where the search should start
Returns:
the zero-based index corresponding to the list item or -1 if the item is not found.
Throws:
java.lang.IndexOutOfBoundsException - if the index is not in the range [0..count()-1].
com.sas.util.InvalidElementTypeException - In some implementations, the objects that are stored may be restricted to a particular type (such as String). (a RuntimeException)

getItems

public java.util.Enumeration getItems(int start,
                                      int end)
Returns an enumeration of a subset of the items from a collection.

Specified by:
getItems in interface StaticOrderedCollectionInterface
Parameters:
start - the index of the first item, zero based.
end - one past the index of the last item to include in the OrderedCollection, zero based. Note that the range of elements returned in the Enumeration does not include the endthe item; this is consistent with substring(start, end) in java.lang.String, for example.
Returns:
an enumeration of the requested elements.
Throws:
java.lang.IndexOutOfBoundsException - if start is not in the range [0, count()-1], or start>last or end is not in the range [0, count()]

getLastIndex

public int getLastIndex(java.lang.Object element,
                        int startIndex)
Returns the index associated with the item (searches backward)

Specified by:
getLastIndex in interface StaticOrderedCollectionInterface
Parameters:
element - the item to search for
startIndex - the position where the search should start
Returns:
the zero-based index corresponding to the list item or -1 if the item is not found.
Throws:
java.lang.IndexOutOfBoundsException - if the index is not in the range [0..count()-1]
com.sas.util.InvalidElementTypeException - In some implementations, the objects that are stored may be restricted to a particular type (such as String). (a RuntimeException)

contains

public boolean contains(java.lang.Object element)
Test for the presence of an object in the collection

Specified by:
contains in interface StaticCollectionInterface
Parameters:
element - The item to search for
Returns:
True if and only if the item exists
Throws:
com.sas.util.InvalidElementTypeException - In some implementations, the objects that are stored may be restricted to a particular type (such as String). (a RuntimeException)

apply

public void apply(com.sas.util.ApplyInterface action)
Applies the 'apply' function in the action to every item in the collection. This is a general way to modify all the items in a collection.

Specified by:
apply in interface StaticCollectionInterface
Parameters:
action - An object that provides a function which takes an object as an argument and then does something to it. The application of the action continues until all items in the the collection have been operated on, of until a call to the action.apply(Object item) method returns ApplyInterface.ABORT

sort

public void sort(com.sas.util.Comparator comparator,
                 int start,
                 int end)
Sorts a range of items from the current collection using the provided comparison function. The sort is performed in place.

Parameters:
comparator - An instance of a subclass of Comparator which provides a compare(Object,Object) function.
start - the index of the first item in the range to sort
end - sort the items between start and (end-1)
See Also:
Comparator

sort

public void sort(com.sas.util.Comparator comparator)
Sorts the items from the current collection using the provided comparison function. The sort is performed in place.

Parameters:
comparator - An instance of a subclass of Comparator which provides a compare(Object,Object) function.
See Also:
Comparator

getDefaultIcon

public com.sas.visuals.IconInterface getDefaultIcon()
Returns the default icon for the current node.

Specified by:
getDefaultIcon in interface NodeIconInterface
Returns:
the default icon for the current node

getExpandedIcon

public com.sas.visuals.IconInterface getExpandedIcon()
Returns the expanded icon for the current node. If the expanded icon is null then the result of getDefaultIcon should be returned.

Specified by:
getExpandedIcon in interface NodeIconInterface
Returns:
the expanded icon for the current node

getSelectedIcon

public com.sas.visuals.IconInterface getSelectedIcon()
Returns the selected icon for the current node. If the selected icon is null then the result of getDefaultIcon should be returned.

Specified by:
getSelectedIcon in interface NodeIconInterface
Returns:
the selected icon for the current node

getExpandedSelectedIcon

public com.sas.visuals.IconInterface getExpandedSelectedIcon()
Sets the expandedSelected icon for the current node. If the expandedSelected icon is null then the result of getExpandedIcon should be returned.

Specified by:
getExpandedSelectedIcon in interface NodeIconInterface
Returns:
the expandedSelected icon for the current node



Copyright © 2009 SAS Institute Inc. All Rights Reserved.