-
-
Notifications
You must be signed in to change notification settings - Fork 22
Reaching a resource of the file system
Through FileSystemItem you can reach a resource of the file system even if it is contained in a nested supported (zip, jar, war, ear, 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. In the example below we show how to retrieve and use a FileSystemItem.
package org.burningwave.core.examples.filesystemitem;
import org.burningwave.core.assembler.ComponentContainer;
import org.burningwave.core.io.FileSystemItem;
public class ResourceReacher {
private static void execute() {
//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();
}
public static void main(String[] args) {
execute();
}
}
Burningwave core is a fully indipendent, advanced, free and open source Java frameworks building library that contains AN EXTREMELY POWERFUL CLASSPATH SCANNER.
To include Burningwave Core library in your projects simply use with Apache Maven:
<dependency>
<groupId>org.burningwave</groupId>
<artifactId>core</artifactId>
<version>12.65.2</version>
</dependency>
To use Burningwave Core as a Java module add the following to your module-info.java
:
requires org.burningwave.core;
ClassFactory
ClassHunter
- In depth look to and configuration guide
- USE CASE: retrieving all classes of the classpath
- USE CASE: retrieving all classes that implement one or more interfaces
- USE CASE: finding all classes that extend a base class
- USE CASE: searching for all classes that have package name that matches a regex
- USE CASE: finding all classes for module name (Java 9 and later)
- USE CASE: finding all annotated classes
- USE CASE: how to scan classes for specific annotations and collect its values
- USE CASE: searching for all classes with a constructor that takes a specific type as first parameter and with at least 2 methods that begin for a given string
- USE CASE: searching for all classes with methods whose name begins for a given string and that takes a specific type as its first parameter
- USE CASE: finding all classes that have at least 2 protected fields