I expect you're going to have to have a getFolder
(?) on your Folder class that you provide the folder name to and it returns the sub-Folder with that name. You can use your path to traverse this in a loop to "cd" to the level you're interested in. Then, I guess a getFile
will return a File
object. You can then rename
your file (though your path would have to be updated to reflect any Folder name changes).
Folder rootFolder = ...;
String[] path = ...;
Folder f = null;
for (int i=0; i<path.length; i++) {
folder = rootFolder.getFolder(path[i]);
}
// f now contains the folder at "path"
// N.B. Haven't handled FolderNotFoundException
File file = f.getFile("Aa2.txt");
file.setName("Aa2.new.txt");
/* Folder#getFolder returning sub-folder for name passed */
public Folder getFolder(String name) {
Folder f = folderCollection.get(name); //no null checks done!
if (f == null) {
... //FolderNotFoundException?
}
return f;
}
Alternatively, you could provide the path (or an encapsulated representation of path) to Folder#getFolderInPath
and handle it all internally to Folder
. This version is iterative, looping over the path array, updating f with that level in the folder path on each iteration. It uses getFolder
from the above:
Folder rootFolder = ...;
String[] path = ...;
Folder f = rootFolder.getFolderInPath(path);
File file = f.getFile("Aa2.txt");
/* Folder#getFolder returning sub-folder for name passed
* This is an "iterative" implementation - looping path array
*/
public Folder getFolderInPath(String[] path) {
Folder f = null;
for (int i=0; i<path.length; i++) {
folder = this.getFolder(path[i]);
}
// f now contains the folder at "path"
// N.B. Haven't handled FolderNotFoundException
return f;
}
This is a recursive version of the above and uses getFolder
(above x 2) again. Needs some explaining - #getFolderInPath
now just delegates to the recursive method #getFolderRecursive
, passing the root values of the recursion (the starting folder ["this"] and the start point in path - index).
Method #getFolderRecursive(Folder
recursively calls itself until the path is traversed (while index < path.length). When it is traversed successfully (FolderNotfoundException?) then index = path.length. At each level, we make the recursive call with the folder found matching the name at that level in the path (governed by index). At the bottom level, the folder found with #getFolder
is the last in the path and should be returned. The recursive methods are then unravelled, passing f up the tree in var folderArtPath
(for clarity). The else
condition sets this variable when the path is traversed and it is carried up the stack in the line folderAtPath =
in the if block:
Folder rootFolder = ...;
String[] path = ...;
Folder f = rootFolder.getFolderInPath(path);
File file = f.getFile("Aa2.txt");
/* Folder#getFolder returning sub-folder for name passed
* This is an "recursive" implementation - digging into the Folder structure
*/
public Folder getFolderInPath(String[] path) {
//Begin recursion with "this2 folder"
return getFolderRecursive(this, path, 0);
}
/* Internal recursive method
*/
private Folder getFolderRecursive(Folder baseFolder, String[] path, int index) {
Folder folderAtPath = null; //This is going to carry the Folder at "path"
if (index < path.length) { //Recursive base condition (are we done?)
//Get folder f with name according to path and index
Folder f = baseFolder.getFolder(path[index])); //FolderNotFoundException?
//Recursively call found folder f with path and index referring
//to next path-part to be used (index+1)
folderAtPath = getFolderRecursive(f, path, index+1);
}
else {
folderAtPath = baseFolder;
}
return folderAtPath;
}
There may be a better way to do this but I can't examine it now. Spent a bit too much time on it but had to correct my mini-cook-up. Recursion's a bit of fun... find a simple example on line and just play with it.
You also may want to make Folder and File have some common interface.