Class IOStreamUtil

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

public class IOStreamUtil extends Object
File and I/O Stream utility
Author:
Leonard Woo
  • 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) throws NullPointerException
      Find and load file from pathname
      Parameters:
      clazz - classloader loading point.
      pathname - relative path.
      Returns:
      file instance.
      Throws:
      NullPointerException - pathname is null.
    • loadFile

      public static File loadFile(String path) throws IllegalArgumentException, NullPointerException, IOException
      Load the target file or create it. if it fails.
      Parameters:
      path - file pathname.
      Returns:
      target file.
      Throws:
      IllegalArgumentException - File could not be loaded.
      NullPointerException - path is null.
      IOException - The path is a folder address or failed to create a sub-folder.
    • 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 instance.
      Returns:
      File input stream if file not found is null.
    • loadReader

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

      public static BufferedReader loadReader(InputStream is)
      Load reader from InputStream.
      Parameters:
      is - Input stream.
      Returns:
      Reader instance.
    • loadReader

      public static BufferedReader loadReader(InputStream is, Charset charset)
      Load input stream to reader with charset
      Parameters:
      is - Input stream.
      charset - Charset see StandardCharsets and ExtendedCharsets.
      Returns:
      Reader instance.
    • 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.
    • readerTo

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

      public static String readerTo(BufferedReader reader, boolean lineStrip, String newLine) throws IOException
      Convert reader to string
      Parameters:
      reader - reader object.
      lineStrip - if true strip every line, false otherwise.
      newLine - newline chars.
      Returns:
      reader content.
      Throws:
      IOException - read close 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 void writeFile(byte[] b, File file) throws IOException
      Write byte array to file If not exist create it
      Parameters:
      b - bytes.
      file - File.
      Throws:
      IOException - I/O error occurs.
    • writeFile

      public static void writeFile(byte[] b, File file, boolean append) throws IOException
      Write byte array to file If not exist create it
      Parameters:
      b - bytes.
      file - File.
      append - file append.
      Throws:
      IOException - I/O error occurs.