Class StreamUtil

java.lang.Object
org.seppiko.commons.utils.StreamUtil

public class StreamUtil extends Object
File and Stream Util
Author:
Leonard Woo
  • Constructor Details

    • StreamUtil

      public StreamUtil()
  • Method Details

    • findFileInputStream

      public static InputStream findFileInputStream(Class<?> clazz, String pathname)
      Find and load file from pathname
      Parameters:
      clazz - classloader loading point
      pathname - relative path
      Returns:
      file input stream if file not found is null
    • findFile

      public static File findFile(Class<?> clazz, String pathname)
      Find and load file from pathname
      Parameters:
      clazz - classloader loading point
      pathname - relative path
      Returns:
      file
    • loadFile

      public static File loadFile(String path) throws IllegalArgumentException, IOException
      Load the target file and create it if it fails.
      Parameters:
      path - file pathname
      Returns:
      target file
      Throws:
      IllegalArgumentException - File could not be loaded
      IOException - The path is a folder address or failed to create a subfolder.
    • getStream

      public static InputStream getStream(Class<?> clazz, String pathname)
      Find and get file steam
      Parameters:
      clazz - classloader loading point
      pathname - relative path
      Returns:
      File input stream if not found is null
    • getStream

      public static InputStream getStream(String filepath)
      Get file stream
      Parameters:
      filepath - File path
      Returns:
      File input stream if file not found is null
    • getStream

      public static InputStream getStream(File file)
      Get file stream
      Parameters:
      file - File object
      Returns:
      File input stream if file not found is null
    • loadString

      public static BufferedReader loadString(String str)
      Load string to reader
      Parameters:
      str - String
      Returns:
      Reader
    • loadReader

      public static BufferedReader loadReader(InputStream is)
      Load reader from InputStream
      Parameters:
      is - Inputstream
      Returns:
      Reader
    • loadReader

      public static BufferedReader loadReader(InputStream is, Charset charset)
      Load inputstram to reader with charset
      Parameters:
      is - Inputstream
      charset - Charest seeStandardCharsets
      Returns:
      Reader
    • exportOutputStream

      public static void exportOutputStream(byte[] bytes, OutputStream os) throws IOException
      export byte array to output stream
      Parameters:
      bytes - byte array
      os - output stream
      Throws:
      IOException - byte array write failed
    • readerToString

      public static String readerToString(BufferedReader reader, boolean lineStrip, boolean ignoreNewLine) throws IOException
      Convert reader to String
      Parameters:
      reader - reader object
      lineStrip - true - remote every line begin and end whitespace
      ignoreNewLine - true - read all content without new line
      Returns:
      reader content without newline char
      Throws:
      IOException - read exception
    • readerToString

      public static String readerToString(BufferedReader reader) throws IOException
      Convert reader all to String
      Parameters:
      reader - reader object
      Returns:
      reader content
      Throws:
      IOException - read exception
    • writeFile

      public static Writer writeFile(String pathname) throws IOException
      Write file If not exist create it
      Parameters:
      pathname - file out path
      Returns:
      file writer
      Throws:
      IOException - file create or file writer exception
    • writeFile

      public static Writer writeFile(File file) throws IOException
      Write file If not exist create it
      Parameters:
      file - file object
      Returns:
      file writer
      Throws:
      IOException - file create or file writer exception
    • writeFile

      public static void writeFile(byte[] b, File file) throws IOException
      Write byte array to file
      Parameters:
      b - bytes
      file - File
      Throws:
      IOException - I/O exception