Package org.seppiko.commons.utils
Class StreamUtil
java.lang.Object
org.seppiko.commons.utils.StreamUtil
public class StreamUtil
extends java.lang.Object
File and Stream Util
- Author:
- Leonard Woo
-
Constructor Summary
Constructors Constructor Description StreamUtil() -
Method Summary
Modifier and Type Method Description static voidcopy(java.io.InputStream is, java.io.OutputStream os)Copy inputstream to outputstreamstatic voidexportOutputStream(byte[] bytes, java.io.OutputStream os)export byte array to output streamstatic java.io.FilefindFile(java.lang.Class<?> clazz, java.lang.String pathname)Find and load file from pathnamestatic java.io.InputStreamfindFileInputStream(java.lang.Class<?> clazz, java.lang.String pathname)Find and load file from pathnamestatic java.io.InputStreamgetStream(java.io.File file)Get file streamstatic java.io.InputStreamgetStream(java.lang.Class<?> clazz, java.lang.String pathname)Find and get file steamstatic java.io.InputStreamgetStream(java.lang.String filepath)Get file streamstatic java.io.BufferedReaderloadReader(java.io.InputStream is)Load reader from InputStreamstatic java.io.BufferedReaderloadReader(java.io.InputStream is, java.nio.charset.Charset charset)Load inputstram to reader with charsetstatic java.io.BufferedReaderloadString(java.lang.String str)Load string to readerstatic java.lang.StringreaderToString(java.io.BufferedReader reader, boolean ignoreNewLine)Convert reader to Stringstatic voidtoFile(byte[] b, java.io.File file)Output to filestatic java.io.WriterwriteFile(java.io.File file)Write file If not exist create itstatic java.io.WriterwriteFile(java.lang.String pathname)Write file If not exist create itMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
StreamUtil
public StreamUtil()
-
-
Method Details
-
findFileInputStream
public static java.io.InputStream findFileInputStream(java.lang.Class<?> clazz, java.lang.String pathname)Find and load file from pathname- Parameters:
clazz- classloader loading pointpathname- relative path- Returns:
- file input stream if file not found is null
-
findFile
public static java.io.File findFile(java.lang.Class<?> clazz, java.lang.String pathname)Find and load file from pathname- Parameters:
clazz- classloader loading pointpathname- relative path- Returns:
- file
-
getStream
public static java.io.InputStream getStream(java.lang.Class<?> clazz, java.lang.String pathname)Find and get file steam- Parameters:
clazz- classloader loading pointpathname- relative path- Returns:
- File input stream if not found is null
-
getStream
public static java.io.InputStream getStream(java.lang.String filepath)Get file stream- Parameters:
filepath- File path- Returns:
- File input stream if file not found is null
-
getStream
public static java.io.InputStream getStream(java.io.File file)Get file stream- Parameters:
file- File object- Returns:
- File input stream if file not found is null
-
loadString
public static java.io.BufferedReader loadString(java.lang.String str)Load string to reader- Parameters:
str- String- Returns:
- Reader
-
loadReader
public static java.io.BufferedReader loadReader(java.io.InputStream is)Load reader from InputStream- Parameters:
is- Inputstream- Returns:
- Reader
-
loadReader
public static java.io.BufferedReader loadReader(java.io.InputStream is, java.nio.charset.Charset charset)Load inputstram to reader with charset- Parameters:
is- Inputstreamcharset- Charest seeStandardCharsets- Returns:
- Reader
-
exportOutputStream
public static void exportOutputStream(byte[] bytes, java.io.OutputStream os) throws java.io.IOExceptionexport byte array to output stream- Parameters:
bytes- byte arrayos- output stream- Throws:
java.io.IOException- byte array write failed
-
readerToString
public static java.lang.String readerToString(java.io.BufferedReader reader, boolean ignoreNewLine) throws java.io.IOExceptionConvert reader to String- Parameters:
reader- ReaderignoreNewLine- true - read all content without new line- Returns:
- reader content without newline char
- Throws:
java.io.IOException- read exception
-
writeFile
public static java.io.Writer writeFile(java.lang.String pathname) throws java.io.IOExceptionWrite file If not exist create it- Parameters:
pathname- file out path- Returns:
- file writer
- Throws:
java.io.IOException- file create or file writer exception
-
writeFile
public static java.io.Writer writeFile(java.io.File file) throws java.io.IOExceptionWrite file If not exist create it- Parameters:
file- file object- Returns:
- file writer
- Throws:
java.io.IOException- file create or file writer exception
-
toFile
public static void toFile(byte[] b, java.io.File file) throws java.io.IOExceptionOutput to file- Parameters:
b- bytesfile- File- Throws:
java.io.IOException- I/O exception
-
copy
public static void copy(java.io.InputStream is, java.io.OutputStream os) throws java.io.IOExceptionCopy inputstream to outputstream- Parameters:
is- InputStreamos- OutputStream- Throws:
java.io.IOException- I/O exception
-