Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
student:java:resources [2019/01/24 11:41] – created bernstdh | student:java:resources [2024/05/05 19:42] (current) – bernstdh | ||
---|---|---|---|
Line 10: | Line 10: | ||
==== Icons ==== | ==== Icons ==== | ||
- | To easiest way to refer to icons in a protable | + | To easiest way to refer to icons in a portable |
<code java> | <code java> | ||
Line 24: | Line 24: | ||
==== Resource Bundles ==== | ==== Resource Bundles ==== | ||
+ | |||
+ | The static '' | ||
<code java> | <code java> | ||
Line 39: | Line 41: | ||
<code java> | <code java> | ||
- | InputStream is = this.getClass().getResource("/ | + | InputStream is = this.getClass().getResourceAsStream("/ |
</ | </ | ||
Line 48: | Line 50: | ||
String | String | ||
</ | </ | ||
+ | |||
+ | |||
+ | ==== Copying Resources to a Temporary Directory ==== | ||
+ | |||
+ | Sometimes it is necessary to copy resources from a %%.jar%% file to a temporary directory on the files system (e.g., if you want to load %%.html%% files that are in a %%.jar%% file into a browser). The following utility class can be used for this purpose. | ||
+ | |||
+ | <code java> | ||
+ | import java.io.IOException; | ||
+ | import java.net.URI; | ||
+ | import java.net.URISyntaxException; | ||
+ | import java.nio.file.Files; | ||
+ | import java.nio.file.FileSystem; | ||
+ | import java.nio.file.FileSystems; | ||
+ | import java.nio.file.Path; | ||
+ | import java.nio.file.Paths; | ||
+ | import java.util.HashMap; | ||
+ | import java.util.List; | ||
+ | import java.util.stream.Stream; | ||
+ | |||
+ | public class ResourceCopier { | ||
+ | |||
+ | /** | ||
+ | * Copy all of the files from a resources package to a temporary directory | ||
+ | * on the file system (whether or not the code and resources are in a .jar | ||
+ | * file or on the file system). | ||
+ | | ||
+ | * This implementation assumes the resource package is a subpackage of the | ||
+ | * package that contains this class (though that can be changed_. | ||
+ | | ||
+ | * @param id The ID to use for the temporary directory | ||
+ | * @param subdir The name of the subpackage that contains the resources | ||
+ | * @return The Path of the temporary directory | ||
+ | * @throws IOException If something goes wrong | ||
+ | * @throws URISyntaxException If something goes wrong | ||
+ | */ | ||
+ | public static Path copyResourcesToTemp(String id, String subdir) | ||
+ | throws IOException, | ||
+ | |||
+ | // Get the location of the ResourceCopier.class file | ||
+ | String canonicalName = ResourceCopier.class.getName(); | ||
+ | String packageName = ResourceCopier.class.getPackageName(); | ||
+ | String className = canonicalName.substring(packageName.length()+1) + " | ||
+ | String thisLocation = ResourceCopier.class.getResource(className).toString(); | ||
+ | |||
+ | // Remove the file name from the location and create a URI | ||
+ | int fileStart = thisLocation.indexOf(className); | ||
+ | String rootURL = thisLocation.substring(0, | ||
+ | URI sourceURI = new URI(rootURL + subdir); | ||
+ | |||
+ | // Get the Path for source files (whether in a .jar file or the file system) | ||
+ | Path sourcePath; | ||
+ | FileSystem fileSystem = null; | ||
+ | if (sourceURI.getScheme().equals(" | ||
+ | fileSystem = FileSystems.newFileSystem(sourceURI, | ||
+ | sourcePath = fileSystem.getPath("/" | ||
+ | } else { | ||
+ | sourcePath = Paths.get(sourceURI); | ||
+ | } | ||
+ | |||
+ | // Get a List of all of the files in the source Path | ||
+ | Stream< | ||
+ | List< | ||
+ | |||
+ | // Create a temporary directory on the local file system | ||
+ | Path temp = Files.createTempDirectory(id); | ||
+ | Path destinationPath = Path.of(temp.toString()); | ||
+ | |||
+ | // Copy all of the files from the source Path to the temporary directory | ||
+ | for (Path file : filesList) { | ||
+ | Path targetFile = Path.of(destinationPath.toString(), | ||
+ | Files.copy(file, | ||
+ | } | ||
+ | |||
+ | files.close(); | ||
+ | if (fileSystem != null) fileSystem.close(); | ||
+ | |||
+ | return destinationPath; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
==== Under the Hood ==== | ==== Under the Hood ==== | ||
- | These technique | + | These technique |