Tutorials (13 Part Series)
1 How to create your own dependency injection framework in Java
2 How to find all the classes of a package in Java
… 9 more parts…
3 How to get field values with Reflection and with direct memory access in Java
4 Optimizing CPU usage by performing tasks in parallel with different priorities in Java
5 How to bind methods or constructors to functional interfaces in Java
6 Generating classes at runtime and invoking their methods with and without the use of Reflection in Java
7 A fast and flexible way to scan the class paths in Java
8 Executing stringified source code in Java
9 How to scan the file system in Java
10 Invoking and handling privates and all other methods of an object with and without the use of Reflection in Java
11 Making applications created with old Java versions work on Java 9 and later versions
12 How to generate and compile sources at runtime in Java
13 Iterating collections and arrays in parallel by setting thread priority
Through FileSystemItem of Burningwave Core library you can reach a resource of the file system even if it is contained in a nested supported (zip, jar, war, ear and jmod) compressed archive and obtain the content of it or other informations such as if it is a folder or a file or a compressed archive or if it is a compressed entry or obtain, if it is a folder or a compressed archive, the direct children or all nested children or a filtered collection of them. You can retrieve a FileSystemItem through an absolute path or through a relative path referred to your classpath by using the PathHelper. FileSystemItems are cached and there will only be one instance of them for an absolute path and you can also clear the cache e reload all informations of a FileSystemItem.
To use FileSystemItem you should simply add the following to your projects dependencies:
In this example we show how to retrieve and use a FileSystemItem:
//Obtaining FileSystemItem through absolute path
FileSystemItem fSI = FileSystemItem.ofPath("C:/Program Files (x86)");
FileSystemItem firstFolderFound = null;
//Obtaining direct children
for (FileSystemItem child : fSI.getChildren()) {
System.out.println("child name:" + child.getAbsolutePath());
if (firstFolderFound == null && child.isFolder()) {
System.out.println(child.getAbsolutePath() + " is a folder: " + child.isFolder());
firstFolderFound = child;
}
}
//Filtering all nested children for extension
for (FileSystemItem child : firstFolderFound.findInAllChildren(
FileSystemItem.Criteria.forAllFileThat(fSIC ->
"txt".equals(fSIC.getExtension()) || "exe".equals(fSIC.getExtension()))
)
){
System.out.println("child name: " + child.getName() + " - child parent: " + child.getParent().getName());
//copy the file to a folder
child.copyTo(System.getProperty("user.home") + "/Desktop/copy");
}
//Obtaining a FileSystemItem through a relative path (in this case we are obtaining a reference to a jar
//contained in an ear that is contained in a zip
fSI = ComponentContainer.getInstance().getPathHelper().getResource(
"/../../src/test/external-resources/libs-for-test.zip/ESC-Lib.ear/APP-INF/lib/jaxb-xjc-2.1.7.jar"
);
System.out.println("is an archive:" + fSI.isArchive());
//This method return true if the file or folder is located inside a compressed archive
System.out.println("is compressed:" + fSI.isCompressed());
//this clear cache
fSI.refresh(true);
//Obtaining direct children
for (FileSystemItem child : fSI.getChildren()) {
System.out.println("child name:" + child.getAbsolutePath());
}
//Obtaining all nested children
for (FileSystemItem child : fSI.getAllChildren()) {
System.out.println("child name:" + child.getAbsolutePath());
}
//Obtaining the content of the resource (once the content is loaded it will be cached)
fSI.toByteBuffer();
Enter fullscreen mode Exit fullscreen mode
In this article we learned how to scan the file system and the complete source of this example is available here.
Tutorials (13 Part Series)
1 How to create your own dependency injection framework in Java
2 How to find all the classes of a package in Java
… 9 more parts…
3 How to get field values with Reflection and with direct memory access in Java
4 Optimizing CPU usage by performing tasks in parallel with different priorities in Java
5 How to bind methods or constructors to functional interfaces in Java
6 Generating classes at runtime and invoking their methods with and without the use of Reflection in Java
7 A fast and flexible way to scan the class paths in Java
8 Executing stringified source code in Java
9 How to scan the file system in Java
10 Invoking and handling privates and all other methods of an object with and without the use of Reflection in Java
11 Making applications created with old Java versions work on Java 9 and later versions
12 How to generate and compile sources at runtime in Java
13 Iterating collections and arrays in parallel by setting thread priority
暂无评论内容