Back Back

NIO.2 File

The SoftSmithy Utility Library provides various utility classes for the NIO.2 File API introduced in Java SE 7.

This trail contains the following samples:

Copy files

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.7.jar:lib/softsmithy-lib-core-0.7.jar samples.nio.file.CopyFilesSample data/first output
    

Extract resources from a JAR

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.7.jar:lib/softsmithy-lib-core-0.7.jar samples.nio.file.ExtractJarResourceSample output
    

Add resources to a ZIP file

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.7.jar:lib/softsmithy-lib-core-0.7.jar samples.nio.file.AddZipResourceSample data/sample-data.zip output data/another /first
    

Get the installation directory

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.7.jar:lib/softsmithy-lib-core-0.7.jar samples.nio.file.GetInstallationDirSample
    
Back Back