java.lang.Object
org.seppiko.commons.utils.StreamUtil
File and Stream Util
- Author:
- Leonard Woo
-
Constructor Summary
Constructors -
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 and create it if it fails.static BufferedReaderLoad reader from InputStreamstatic BufferedReaderloadReader(InputStream is, Charset charset) Load inputstram to reader with charsetstatic BufferedReaderloadString(String str) Load string to readerstatic StringreaderTo(BufferedReader reader, boolean lineStrip, String newLine) Convert reader to stringstatic StringreaderToString(BufferedReader reader) Convert reader all to Stringstatic StringreaderToString(BufferedReader reader, boolean lineStrip, boolean ignoreNewLine) Convert reader to Stringstatic ReaderRead filestatic ReaderRead filestatic voidWrite byte array to file If not exist create itstatic voidWrite byte array to file If not exist create itstatic WriterWrite file If not exist create itstatic WriterWrite file If not exist create it
-
Constructor Details
-
StreamUtil
public StreamUtil()
-
-
Method Details
-
findFileInputStream
Find and load file from pathname- Parameters:
clazz- classloader loading pointpathname- relative path- Returns:
- file input stream if file not found is null
-
findFile
Find and load file from pathname- Parameters:
clazz- classloader loading pointpathname- relative path- Returns:
- file
-
loadFile
Load the target file and create it if it fails.- Parameters:
path- file pathname- Returns:
- target file
- Throws:
IllegalArgumentException- File could not be loadedIOException- The path is a folder address or failed to create a subfolder.
-
getStream
Find and get file steam- Parameters:
clazz- classloader loading pointpathname- 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 object- Returns:
- File input stream if file not found is null
-
loadString
Load string to reader- Parameters:
str- String- Returns:
- Reader
-
loadReader
Load reader from InputStream- Parameters:
is- Inputstream- Returns:
- Reader
-
loadReader
Load inputstram to reader with charset- Parameters:
is- Inputstreamcharset- Charest seeStandardCharsets- Returns:
- Reader
-
exportOutputStream
export byte array to output stream- Parameters:
bytes- byte arrayos- 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 objectlineStrip- if true remote every line begin and end whitespace, false otherwiseignoreNewLine- if true read all content without new line, false otherwise- Returns:
- reader content without newline char
- Throws:
IOException- read close exception
-
readerTo
Convert reader to string- Parameters:
reader- reader objectlineStrip- if true strip every line, false otherwisenewLine- newline chars- Returns:
- reader content
-
readerToString
Convert reader all to String- Parameters:
reader- reader object- Returns:
- reader content
- Throws:
IOException- read exception
-
readFile
Read file- Parameters:
pathname- file pathname- Returns:
- file reader
- Throws:
IOException- file read exception
-
readFile
Read file- Parameters:
file- file object- Returns:
- file reader
- Throws:
IOException- file read exception
-
writeFile
Write file If not exist create it- Parameters:
pathname- file pathname- Returns:
- file writer
- Throws:
IOException- file create or file writer exception
-
writeFile
Write file If not exist create it- Parameters:
file- file object- Returns:
- file writer
- Throws:
IOException- file create or file writer exception
-
writeFile
Write byte array to file If not exist create it- Parameters:
b- bytesfile- File- Throws:
IOException- I/O exception
-
writeFile
Write byte array to file If not exist create it- Parameters:
b- bytesfile- Fileappend- file append- Throws:
IOException- I/O exception
-