NIO File
The SoftSmithy Utility Library provides
various utility classes
for the NIO File API introduced in Java SE 7.
The SoftSmithy Utility Library distribution archive
contains all SoftSmithy Utility Library jars, Javadoc, tutorial and samples.
This trail contains the following samples:
Below is the code from
CopyFilesSample.java
that copies some files to a target directory.
This sample uses the CopyFileVisitor
to recursively copy a source directory to a target directory. The CopyFileVisitor implements the
FileVisitor interface and thus can be use with one of the
Files.walkFileTree methods.
For convenience there is the static CopyFileVisitor.copy
method.
To run this sample, use:
cd docs/samples/dist
java -cp softsmithy-lib-samples-0.4.jar:lib/softsmithy-lib-core-0.4.jar samples.nio.file.CopyFilesSample data/first output
Below is the code from
ExtractJarResourceSample.java
that extracts a resource from the JAR containing the ExtractJarResourceSample.class file to a target directory.
Oracle's Java SE 7 Runtime comes with a
FileSystemProvider implementation for JAR and ZIP files.
This FileSystemProvider allows you to work with JAR and ZIP files in a similar way as with the local file system.
The JarFiles utility class
provides some utility methods to work with JAR/ ZIP files.
With the help of CopyFileVisitor
you can easily recursively extract/ add directories from/ to JAR or ZIP files, as CopyFileVisitor works across
file systems.
To run this sample, use:
cd docs/samples/dist
java -cp softsmithy-lib-samples-0.4.jar:lib/softsmithy-lib-core-0.4.jar samples.nio.file.ExtractJarResourceSample output
Below is the code from
AddZipResourceSample.java
that adds a source directory to a ZIP file.
This sample is similar to Extract resources from a JAR, but adds resources
to a ZIP file instead of extracting resources from a JAR.
In addition, the PathUtils
utility class is used to resolve paths across file systems.
To run this sample, use:
cd docs/samples/dist
java -cp softsmithy-lib-samples-0.4.jar:lib/softsmithy-lib-core-0.4.jar samples.nio.file.AddZipResourceSample data/sample-data.zip output data/another /first
If you unpack the SoftSmithy Utility Library distribution archive, you can find the JAR containing the samples at the
following location:
<install-dir>/docs/samples/dist/softsmithy-lib-samples-0.4.jar
<install-dir>/docs/samples/dist/lib/<other jars>
Below is the code from
GetInstallationDirSample.java
that gets the <install-dir>.
In this sample, JarFiles.getJarURI
is used to get the installation directory.
To run this sample, use:
cd docs/samples/dist
java -cp softsmithy-lib-samples-0.4.jar:lib/softsmithy-lib-core-0.4.jar samples.nio.file.GetInstallationDirSample