java.lang.Object
org.seppiko.commons.utils.StreamUtil
File and Stream Util
- Author:
- Leonard Woo
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidexportOutputStream(byte[] bytes, OutputStream os) export byte array to output streamstatic FileFind and load file from pathnamestatic InputStreamfindFileInputStream(Class<?> clazz, String pathname) Find and load file from pathnamestatic InputStreamGet file streamstatic InputStreamFind and get file steamstatic InputStreamGet file streamstatic FileLoad the target file or create it.static BufferedReaderLoad reader from InputStream.static BufferedReaderloadReader(InputStream is, Charset charset) Load input stream to reader with charsetstatic BufferedReaderloadReader(String str) Load string to readerstatic StringreaderTo(BufferedReader reader, boolean lineStrip, boolean ignoreNewLine) Convert reader to String and closestatic StringreaderTo(BufferedReader reader, boolean lineStrip, String newLine) Convert reader to stringstatic StringreaderToString(BufferedReader reader) Convert reader all to Stringstatic voidWrite byte array to file If not exist create itstatic voidWrite byte array to file If not exist create it
-
Method Details
-
findFileInputStream
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
Find and load file from pathname- Parameters:
clazz- classloader loading point.pathname- relative path.- Returns:
- file instance.
- Throws:
NullPointerException- pathname isnull.
-
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 isnull.IOException- The path is a folder address or failed to create a sub-folder.
-
getStream
Find and get file steam- Parameters:
clazz- classloader loading point.pathname- relative path.- Returns:
- File input stream if not found is
null.
-
getStream
Get file stream- Parameters:
filepath- File path.- Returns:
- File input stream if file not found is
null.
-
getStream
Get file stream- Parameters:
file- File instance.- Returns:
- File input stream if file not found is
null.
-
loadReader
Load string to reader- Parameters:
str- String.- Returns:
- Reader instance.
-
loadReader
Load reader from InputStream.- Parameters:
is- Input stream.- Returns:
- Reader instance.
-
loadReader
Load input stream to reader with charset- Parameters:
is- Input stream.charset- Charset seeStandardCharsetsandExtendedCharsets.- Returns:
- Reader instance.
-
exportOutputStream
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
Convert reader all to String- Parameters:
reader- reader object.- Returns:
- reader content.
- Throws:
IOException- read exception.
-
writeFile
Write byte array to file If not exist create it- Parameters:
b- bytes.file- File.- Throws:
IOException- I/O error occurs.
-
writeFile
Write byte array to file If not exist create it- Parameters:
b- bytes.file- File.append- file append.- Throws:
IOException- I/O error occurs.
-