com.h3c.util

## Class FileUtils

- java.lang.Object
- - com.h3c.util.FileUtils

-     public final class FileUtils
      extends Object

  Miscellaneous utilities for manipulating files.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `FileUtils()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static String[]` | `getResourceListing(Class<?> clazz, String path)` List directory contents for a resource folder.
    `static String` | `read(String file)` Consume the content of the specified text file and return it as a string.
    `static void` | `replaceERE(String file, String ere, String replacement)` Utility to replace a given ERE in the specified file with a replacement string.
    `static boolean` | `replaceFile(String file, String toFile)` Safely moves one file over another.
    `static void` | `write(InputStream fileStream, File file)` Write the given input stream of data to the specified file
    `static void` | `write(String file, String data)` Write the given data into the specified text file.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### FileUtils

          public FileUtils()

  - ### Method Detail

    - #### replaceERE

          public static void replaceERE(String file,
                        String ere,
                        String replacement)
                                 throws IOException

      Utility to replace a given ERE in the specified file with a replacement string.
      Parameters:
        `file` - file to search and replace patterns within
        `ere` - extended regular expression to be replaced
        `replacement` - replacement string
      Throws:
        `IOException` - if issues encountered while reading original or writing replacement

    - #### replaceFile

          public static boolean replaceFile(String file,
                            String toFile)

      Safely moves one file over another.
      Parameters:
        `file` - source file
        `toFile` - target file to which the source file should be moved
      Returns:
        true if the replacement succeeded; false otherwise

    - #### read

          public static String read(String file)
                             throws IOException

      Consume the content of the specified text file and return it as a string.
      Parameters:
        `file` - file path of the file to be read
      Returns:
        string containing file content
      Throws:
        `IOException` - if issues encountered while reading file

    - #### write

          public static void write(String file,
                   String data)
                            throws IOException

      Write the given data into the specified text file.
      Parameters:
        `file` - file path of the file to be written
        `data` - to be written
      Throws:
        `IOException` - if issues encountered while reading file

    - #### write

          public static void write(InputStream fileStream,
                   File file)
                            throws IOException

      Write the given input stream of data to the specified file
      Parameters:
        `fileStream` - the input stream containing the data to be written
        `file` - The file to be written to
      Throws:
        `IOException` - if issues encountered while writing the file

    - #### getResourceListing

          public static String[] getResourceListing(Class<?> clazz,
                                    String path)
                                             throws URISyntaxException,
                                                    IOException

      List directory contents for a resource folder. Not recursive. This is basically a brute-force implementation. Works for regular files and also JARs. Based on code authored by Greg Briggs.
      Parameters:
        `clazz` - Any java class that lives in the same place as the resources you want.
        `path` - Should end with "/", but not start with one.
      Returns:
        Just the name of each member item, not the full paths.
      Throws:
        `URISyntaxException` - if the given path forms bad URL
        `IOException` - if issues arise reading the requested resource
        `IllegalArgumentException` - if path starts with a slash

Copyright © 2014. All Rights Reserved.