13) Create, View and Sort directories. |
Creating directories using Xload is easy and there are two ways to do it. The following code demonstrates.
XloadManager xman = new XloadManager(request); XloadDirectory dir = xman.createDirectory("parentDir", "dir");
|
where:
request - HttpServletRequest object.
parentDir - Parent directory to create new directory in.
dir - Name of the directory to create.
OR
XloadManager xman = new XloadManager(request); XloadDirectory d = xman.getDirectory("parentDir"); XloadDirectory dir = d.createDirectory("dir");
|
where:
request - HttpServletRequest object.
parentDir - Parent directory to create new directory in.
dir - Name of the directory to create.
Notice how relative paths can be used because we are using an XloadManager that wraps an HttpServletRequest object.
To view what is inside a directory you can use a number of techniques to view information and to iterate through the directory. The following code demonstrates how to find out useful information about the directory. This is not an exhaustive list of what is available within Xload when accessing directories. Notice the optional use of the default XloadManager constructor for the following examples which requires the use of absolute paths.
XloadManager xman = new XloadManager(); XloadDirectory dir = xman.getDirectory("c:\dir"); float megabyteSize = dir.getMegabyteSize(false); int size = dir.getSize(); String absolutePath = dir.getDirectoryPath(); String name = dir.getName(); boolean exists = dir.exists(); boolean fileExists = dir.fileExists("file");
|
where:
dir - Directory to find information on.
Xload provides an iteration technique to iterate through the directory contents. The code below demonstrates.
XloadManager xman = new XloadManager(); XloadDirectory dir = xman.getDirectory("c:\dir"); while(dir.hasMore()){ XloadDirectoryItem item = dir.getNext(); if(item.isFile()){ XloadFile file = (XloadFile)item; //deal with file. }else{ XloadDirectory direct = (XloadDirectory)item; //deal with directory. } }
|
Two methods (setOmmitFiles(true) and setOmmitDirectories(true)) are provided by Xload that control what is included in the iteration process. For example, if setOmmitFiles(true) was called then the iteration process would not include any files etc. A reset() method is provided to reset the iteration curser back to the beginning so that the process can be restarted.
The setOmmitFiles() and the setOmmitDirectories() methods also affect the getSize() method which returns the number of items within the directory.
Xload provides a sophisticated mechanism for sorting items inside a directory. The method to use is sortBy() (see table below for parameter descriptions) and will return the items in the prescribed order when iterated over. This is demonstrated below.
XloadManager xman = new XloadManager(); XloadDirectory dir = xman.getDirectory("c:\dir"); dir.sortBy(1, true); while(dir.hasMore()){ XloadDirectoryItem item = dir.getNext(); if(item.isFile()){ XloadFile file = (XloadFile)item; //deal with file. }else{ XloadDirectory direct = (XloadDirectory)item; //deal with directory. } }
|
The method sortBy(code, ascending) parameters are described below:
code:
code(int): |
Sorted by: |
1 |
name |
2 |
size |
3 |
last modified date |
ascending:
ascending (boolean): |
Sorted by: |
true |
Sort in ascending order(1,2,3) |
false |
Sort in descending order(3,2,1) |
IMPORTANT:- Any files that are returned from the getNext() method will be of the correct type (i.e. a file deployment or an ordinary file) and will contain the correct state. Remember, file deployments only exist as file deployments for the life of the XloadManager object that they were created from. |
© Gubutech(Xload) 2006 (v1.2)