Back Back

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:

Copy files

Below is the code from CopyFilesSample.java that copies some files to a target directory.
            
  1. public void copyFiles(Path srcPath, Path targetDirPath) throws IOException {  
  2.     // Adds the src directory name to the target directory. You can omit this if you just want to copy the contents.  
  3.     Path finalTargetPath = targetDirPath.resolve(srcPath.getFileName());  
  4.     Files.createDirectories(finalTargetPath);  
  5.   
  6.     CopyFileVisitor.copy(srcPath, finalTargetPath);  
  7. }  
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
    

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.
            
  1. public void extractResource(String resourcePathString, Path targetDirPath) throws IOException, URISyntaxException {  
  2.     URI jarURI = JarFiles.getJarURI(ExtractJarResourceSample.class);  
  3.     try (FileSystem jarFS = JarFiles.newJarFileSystem(jarURI)) {  
  4.         Path resourcePath = jarFS.getPath(resourcePathString);  
  5.   
  6.         CopyFileVisitor.copy(resourcePath, targetDirPath);  
  7.     }  
  8. }  
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
    

Add resources to a ZIP file

Below is the code from AddZipResourceSample.java that adds a source directory to a ZIP file.
            
  1. public void addResource(Path zipPath, Path targetDirPath, Path srcPath, String targetInZipPathString) throws IOException {  
  2.     Path targetZipPath = copyZipFile(zipPath, targetDirPath);  
  3.   
  4.     try (FileSystem jarFS = JarFiles.newJarFileSystem(targetZipPath.toUri())) {  
  5.         Path targetInZipPath = jarFS.getPath(targetInZipPathString);  
  6.   
  7.         // Adds the src directory name to the zip. You can omit this if you just want to copy the contents.  
  8.         Path finalTargetInZipPath = PathUtils.resolve(targetInZipPath, srcPath.getFileName());  
  9.         Files.createDirectories(finalTargetInZipPath);  
  10.   
  11.         CopyFileVisitor.copy(srcPath, finalTargetInZipPath);  
  12.     }  
  13. }  
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
    

Get the installation directory

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>.
            
  1. public void printInstallDirPath() throws IOException, URISyntaxException {  
  2.     URI jarURI = JarFiles.getJarURI(GetInstallationDirSample.class);  
  3.     Path jarPath = Paths.get(jarURI);  
  4.     Path installDirPath = jarPath.getParent().getParent().getParent().getParent();  
  5.       
  6.     System.out.println(installDirPath);  
  7. }  
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
    
Back Back