Class PathUtils


  • public final class PathUtils
    extends Object
    This utility class provides a getNextPath method to acquire the next file path in sequence for the specified base name and extension in the indicated target folder. If the target folder already contains at least one file that matches the specified base name and extension, the algorithm used to select the next path will always return a path whose index is one more than the highest index that currently exists. (If a single file with no index is found, its implied index is 0.)

    Example usage of getNextPath

         ...
    
         /*
          * This example gets the next path in sequence for base name `artifact`
          * and extension `txt` in the TestNG output directory.
          *
          * For purposes of this example, the output directory already contains
          * the following files: `artifact.txt`, `artifact-3.txt`
          */
    
         Path collectionPath = Paths.get(testContext.getOutputDirectory());
         // => C:\git\my-project\test-output\Default suite
    
         Path artifactPath;
         try {
             artifactPath = PathUtils.getNextPath(collectionPath, "artifact", "txt");
             // => C:\git\my-project\test-output\Default suite\artifact-4.txt
         } catch (IOException e) {
             provider.getLogger().info("Unable to get output path; no artifact was captured", e);
             return;
         }
    
         ...
     
    • Method Detail

      • getNextPath

        public static Path getNextPath​(Path targetPath,
                                       String baseName,
                                       String extension)
                                throws IOException
        Get the next available path in sequence for the specified base name and extension in the specified folder.
        Parameters:
        targetPath - path to target directory for the next available path in sequence
        baseName - base name for the path sequence
        extension - extension for the path sequence
        Returns:
        the next available path in sequence
        Throws:
        IOException - if an I/O error is thrown when accessing the starting file.
      • getBaseDir

        public static String getBaseDir()
        Get project base directory.
        Returns:
        project base directory
      • prepend

        public static String[] prepend​(String prefix,
                                       String... strings)
        Prepend the specified string to the indicated array.
        Parameters:
        prefix - string to be prepended
        strings - target string array
        Returns:
        target array prefixed with the specified string
      • append

        public static String[] append​(String suffix,
                                      String... strings)
        Append the specified string to the indicated array.
        Parameters:
        suffix - string to be appended
        strings - target string array
        Returns:
        target array with the specified string appended