FolderResource represents a single folder in the system.
It always corresponds to a unique
FolderDescriptor and - just like
FileResource - is managed by a
FolderResource is the equivalent of a directory in the FileManagerModule virtual file system.
// retrieving a folder resource directly FolderResource folderResource = fileManager.getFolderResource( folderDescriptor ); // retrieving the folder that contains a file resource FileResource fileResource = fileManager.getFileResource( fileDescriptor ); folderResource = fileResource.getFolderResource();
FolderResource is the interface that applications should use.
You should avoid using specific implementations directly.
A folder resource object is only a valid handle to a folder.
Checking if a physical folder exists is done with
Some implementations might always return
In most cases it should not be necessary to explicitly create a folder.
FileRepository implementations should transparently create all necessary folder resources when saving files.
If the underlying storage supports it and should you require it, a folder can be created explicitly using:
boolean created = folderResource.create();
The return value indicates if the folder has been created.
A return value of
true implies that subsequent calls to
exists() should also return
Even if the storage engine does not support the concept of standalone folders, calls to
A folder can contain both file resources and sub folder resources.
FolderResource has several methods to retrieve the folder content.
Collection<FileResource> files = folderResource.listFiles();
Collection<FolderResource> folders = folderResource.listFolders();
Collection<FileRepositoryResource> resources = folderResource.listResources( false );
Collection<FileResource> files = folderResource.listResources( true, FileResource.class );
Single folder resources can be accessed through the relative path.
FileResource file = folderResource.getFileResource( "myfile.txt" ); FolderResource subFolder = folderResource.getFolderResource( "subFolder/" );
Any leading / will be ignored, the example using
subFolder/ is the same as using
The relative path is allowed to contain multiple sub-folders:
FileResource file = folderResource.getFileResource( "2019/06/15/13h00.log" );
It is possible to search a folder for resources using
findResources() with a valid ANT pattern.
This will return all resources matching the pattern.
folderResource.findFiles( "/**/*.txt" )
folderResource.findFiles( "2019/06/15/*.log" )
folderResource.findResources( "/*/tmp/", FolderResource.class )
|Using too many wildcards in ANT patterns can make searching for files in large repositories very slow. Always create your pattern as specific as possible.|
You add a file to a folder by getting a
FileResource inside the folder and writing to it.
FileResource file = folderResource.getFileResource( "subFolder/myfile.txt" ); file.copyFrom( data );
For most repository implementations, an actual physical file is only allocated when data is written to the resource.
If you want to generate a unique
FileResource to write to, you can use
FileResource file = folderResource.createFileResource(); file.copyFrom( data );
This will create a unique file that can be written to, as a direct child of the folder resource.
You create a sub-folder by getting a
FolderResource inside the folder.
FolderResource subFolder = folderResource.getFolderResource( "subFolder/" );
A folder is mainly a grouping structure and not all repository implementations support standalone folders.
If you explicitly want to create the sub-folder, you can use
The return value will be
false if the folder already exists or could not be created (for example because the storage engine does not support empty folders).
Deleting a folder is done by calling
boolean argument indicates if the folder should be deleted if it is not empty.
false will only delete the folder if it is empty,
true will always delete the folder (deleting first all resources it contains).
If you want to clear the folder but not necessarily remove the folder itself, you can use
This will always delete any child folders, even if they are not empty.
As with calls to