public class IoUtil extends NioUtil
DEFAULT_BUFFER_SIZE, DEFAULT_LARGE_BUFFER_SIZE, DEFAULT_MIDDLE_BUFFER_SIZE, DEFAULT_SMALL_BUFFER_SIZE, EOF
Constructor and Description |
---|
IoUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
closeIfPossible(Object obj)
尝试关闭指定对象
判断对象如果实现了 AutoCloseable ,则调用之 |
static void |
closeQuietly(AutoCloseable... closeables)
按照给定顺序连续关闭一系列对象
这些对象必须按照顺序关闭,否则会出错。 |
static boolean |
contentEquals(InputStream input1,
InputStream input2)
对比两个流内容是否相同
内部会转换流为 BufferedInputStream |
static boolean |
contentEquals(Reader input1,
Reader input2)
对比两个Reader的内容是否一致
内部会转换流为 BufferedInputStream |
static boolean |
contentEqualsIgnoreEOL(Reader input1,
Reader input2)
对比两个流内容是否相同,忽略EOL字符
内部会转换流为 BufferedInputStream |
static long |
copy(FileInputStream in,
FileOutputStream out)
拷贝文件流,使用NIO
|
static long |
copy(InputStream in,
OutputStream out)
拷贝流,使用默认Buffer大小,拷贝后不关闭流
|
static long |
copy(InputStream in,
OutputStream out,
int bufferSize)
拷贝流,拷贝后不关闭流
|
static long |
copy(InputStream in,
OutputStream out,
int bufferSize,
long count,
StreamProgress streamProgress)
拷贝流,拷贝后不关闭流
|
static long |
copy(InputStream in,
OutputStream out,
int bufferSize,
StreamProgress streamProgress)
拷贝流,拷贝后不关闭流
|
static long |
copy(Reader reader,
Writer writer)
将Reader中的内容复制到Writer中 使用默认缓存大小,拷贝后不关闭Reader
|
static long |
copy(Reader reader,
Writer writer,
int bufferSize)
将Reader中的内容复制到Writer中,拷贝后不关闭Reader
|
static long |
copy(Reader reader,
Writer writer,
int bufferSize,
long count,
StreamProgress streamProgress)
将Reader中的内容复制到Writer中,拷贝后不关闭Reader
|
static long |
copy(Reader reader,
Writer writer,
int bufferSize,
StreamProgress streamProgress)
将Reader中的内容复制到Writer中,拷贝后不关闭Reader
|
static void |
flush(Flushable flushable)
从缓存中刷出数据
|
static int |
length(InputStream in)
获取流长度,对于文件流,会调用
FileInputStream.available() 方法,对于其他流,返回-1对于网络流,available可能为分段大小,所以返回-1 |
static LineIter |
lineIter(InputStream in,
Charset charset)
返回行遍历器
LineIterator it = null;
try {
it = IoUtil.lineIter(in, CharsetUtil.CHARSET_UTF_8);
while (it.hasNext()) {
String line = it.nextLine();
// do something with line
}
} finally {
it.close();
}
|
static LineIter |
lineIter(Reader reader)
返回行遍历器
LineIterator it = null;
try {
it = IoUtil.lineIter(reader);
while (it.hasNext()) {
String line = it.nextLine();
// do something with line
}
} finally {
it.close();
}
|
static void |
nullSafeClose(Closeable closeable)
关闭
关闭失败抛出 IOException 异常 |
static FastByteArrayOutputStream |
read(InputStream in)
从流中读取内容,读到输出流中,读取完毕后关闭流
|
static FastByteArrayOutputStream |
read(InputStream in,
boolean isClose)
从流中读取内容,读到输出流中,读取完毕后可选是否关闭流
|
static String |
read(InputStream in,
Charset charset)
从流中读取内容,读取完毕后关闭流
|
static String |
read(Reader reader)
从Reader中读取String,读取完毕后关闭Reader
|
static String |
read(Reader reader,
boolean isClose)
从
Reader 中读取String |
static byte[] |
readBytes(InputStream in)
从流中读取bytes,读取完毕后关闭流
|
static byte[] |
readBytes(InputStream in,
boolean isClose)
从流中读取bytes
|
static byte[] |
readBytes(InputStream in,
int length)
读取指定长度的byte数组,不关闭流
|
static String |
readHex(InputStream in,
int length,
boolean toLowerCase)
读取16进制字符串
|
static void |
readLines(InputStream in,
Charset charset,
SerConsumer<String> lineHandler)
按行读取数据,针对每行的数据做处理
|
static <T extends Collection<String>> |
readLines(InputStream in,
Charset charset,
T collection)
从流中读取内容
|
static void |
readLines(Reader reader,
SerConsumer<String> lineHandler)
按行读取数据,针对每行的数据做处理
Reader 自带编码定义,因此读取数据的编码跟随其编码。 |
static <T extends Collection<String>> |
readLines(Reader reader,
T collection)
从Reader中读取内容
|
static <T> T |
readObj(InputStream in,
Class<?>... acceptClasses)
从流中读取对象,即对象的反序列化,读取后不关闭流
注意!!!
|
static FastByteArrayOutputStream |
readTo(InputStream in,
Predicate<Integer> predicate)
从流中读取内容,直到遇到给定token满足
Predicate.test(Object) |
static FastByteArrayOutputStream |
readToToken(InputStream in,
int token)
从流中读取内容,直到遇到给定token
|
static String |
readUtf8(InputStream in)
从流中读取UTF8编码的内容
|
static void |
readUtf8Lines(InputStream in,
SerConsumer<String> lineHandler)
按行读取UTF-8编码数据,针对每行的数据做处理
|
static <T extends Collection<String>> |
readUtf8Lines(InputStream in,
T collection)
从流中读取内容,使用UTF-8编码
|
static InputStream |
toAvailableStream(InputStream in)
将指定
InputStream 转换为InputStream.available() 方法可用的流。 |
static BomReader |
toBomReader(InputStream in)
|
static BufferedInputStream |
toBuffered(InputStream in)
|
static BufferedInputStream |
toBuffered(InputStream in,
int bufferSize)
|
static BufferedOutputStream |
toBuffered(OutputStream out)
|
static BufferedOutputStream |
toBuffered(OutputStream out,
int bufferSize)
|
static BufferedReader |
toBuffered(Reader reader)
|
static BufferedReader |
toBuffered(Reader reader,
int bufferSize)
|
static BufferedWriter |
toBuffered(Writer writer)
|
static BufferedWriter |
toBuffered(Writer writer,
int bufferSize)
|
static InputStream |
toMarkSupport(InputStream in)
|
static Reader |
toMarkSupport(Reader reader)
|
static PushbackReader |
toPushBackReader(Reader reader,
int pushBackSize)
|
static PushbackInputStream |
toPushbackStream(InputStream in,
int pushBackSize)
|
static BufferedReader |
toReader(InputStream in,
Charset charset)
获得一个Reader
|
static String |
toStr(ByteArrayOutputStream out,
Charset charset)
ByteArrayOutputStream 转换为String |
static ByteArrayInputStream |
toStream(byte[] content)
byte[] 转为
ByteArrayInputStream |
static ByteArrayInputStream |
toStream(ByteArrayOutputStream out)
|
static ByteArrayInputStream |
toStream(FastByteArrayOutputStream out)
|
static InputStream |
toStream(File file)
文件转为
InputStream |
static InputStream |
toStream(Path path)
文件转为
InputStream |
static ByteArrayInputStream |
toStream(String content,
Charset charset)
String 转为流
|
static BufferedReader |
toUtf8Reader(InputStream in)
获得一个文件读取器,默认使用 UTF-8 编码
|
static ByteArrayInputStream |
toUtf8Stream(String content)
String 转为UTF-8编码的字节流流
|
static OutputStreamWriter |
toUtf8Writer(OutputStream out)
获得一个Writer,默认编码UTF-8
|
static OutputStreamWriter |
toWriter(OutputStream out,
Charset charset)
获得一个Writer
|
static void |
write(OutputStream out,
boolean isCloseOut,
byte[] content)
将byte[]写到流中
|
static void |
write(OutputStream out,
byte[] content)
将byte[]写到流中,并关闭目标流
|
static void |
writeClose(OutputStream out,
byte[] content)
将byte[]写到流中,并关闭目标流
|
static void |
writeObjects(OutputStream out,
boolean isCloseOut,
Object... contents)
将多部分内容写到流中
|
static void |
writeStrs(OutputStream out,
Charset charset,
boolean isCloseOut,
CharSequence... contents)
将多部分内容写到流中,自动转换为字符串
|
static void |
writeUtf8(OutputStream out,
boolean isCloseOut,
CharSequence... contents)
将多部分内容写到流中,自动转换为UTF-8字符串
|
public static long copy(Reader reader, Writer writer) throws IORuntimeException
reader
- Readerwriter
- WriterIORuntimeException
- IO异常public static long copy(Reader reader, Writer writer, int bufferSize) throws IORuntimeException
reader
- Readerwriter
- WriterbufferSize
- 缓存大小IORuntimeException
- IO异常public static long copy(Reader reader, Writer writer, int bufferSize, StreamProgress streamProgress) throws IORuntimeException
reader
- Readerwriter
- WriterbufferSize
- 缓存大小streamProgress
- 进度处理器IORuntimeException
- IO异常public static long copy(Reader reader, Writer writer, int bufferSize, long count, StreamProgress streamProgress) throws IORuntimeException
reader
- Reader,非空writer
- Writer,非空bufferSize
- 缓存大小,-1表示默认count
- 最大长度,-1表示无限制streamProgress
- 进度处理器,null
表示无IORuntimeException
- IO异常public static long copy(InputStream in, OutputStream out) throws IORuntimeException
in
- 输入流out
- 输出流IORuntimeException
- IO异常public static long copy(InputStream in, OutputStream out, int bufferSize) throws IORuntimeException
in
- 输入流out
- 输出流bufferSize
- 缓存大小IORuntimeException
- IO异常public static long copy(InputStream in, OutputStream out, int bufferSize, StreamProgress streamProgress) throws IORuntimeException
in
- 输入流out
- 输出流bufferSize
- 缓存大小streamProgress
- 进度条IORuntimeException
- IO异常public static long copy(InputStream in, OutputStream out, int bufferSize, long count, StreamProgress streamProgress) throws IORuntimeException
in
- 输入流out
- 输出流bufferSize
- 缓存大小count
- 总拷贝长度,-1表示无限制streamProgress
- 进度条IORuntimeException
- IO异常public static long copy(FileInputStream in, FileOutputStream out) throws IORuntimeException
in
- 输入out
- 输出IORuntimeException
- IO异常public static BufferedReader toUtf8Reader(InputStream in)
in
- 输入流public static BomReader toBomReader(InputStream in)
in
- InputStream
BomReader
public static BufferedReader toReader(InputStream in, Charset charset)
in
- 输入流charset
- 字符集public static OutputStreamWriter toUtf8Writer(OutputStream out)
out
- 输入流public static OutputStreamWriter toWriter(OutputStream out, Charset charset)
out
- 输入流charset
- 字符集public static String readUtf8(InputStream in) throws IORuntimeException
in
- 输入流IORuntimeException
- IO异常public static String read(InputStream in, Charset charset) throws IORuntimeException
in
- 输入流,读取完毕后关闭流charset
- 字符集IORuntimeException
- IO异常public static FastByteArrayOutputStream read(InputStream in) throws IORuntimeException
in
- 输入流IORuntimeException
- IO异常public static FastByteArrayOutputStream read(InputStream in, boolean isClose) throws IORuntimeException
in
- 输入流isClose
- 读取完毕后是否关闭流IORuntimeException
- IO异常public static String read(Reader reader) throws IORuntimeException
reader
- ReaderIORuntimeException
- IO异常public static String read(Reader reader, boolean isClose) throws IORuntimeException
Reader
中读取Stringreader
- Reader
isClose
- 是否关闭Reader
IORuntimeException
- IO异常public static byte[] readBytes(InputStream in) throws IORuntimeException
in
- InputStream
IORuntimeException
- IO异常public static byte[] readBytes(InputStream in, boolean isClose) throws IORuntimeException
in
- InputStream
isClose
- 是否关闭输入流IORuntimeException
- IO异常public static byte[] readBytes(InputStream in, int length) throws IORuntimeException
in
- InputStream
,为null
返回null
length
- 长度,小于等于0返回空byte数组IORuntimeException
- IO异常public static String readHex(InputStream in, int length, boolean toLowerCase) throws IORuntimeException
in
- InputStream
length
- 长度toLowerCase
- true 传换成小写格式 , false 传换成大写格式IORuntimeException
- IO异常public static <T> T readObj(InputStream in, Class<?>... acceptClasses) throws IORuntimeException, HutoolException
注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!
T
- 读取对象的类型in
- 输入流acceptClasses
- 读取对象类型IORuntimeException
- IO异常HutoolException
- ClassNotFoundException包装public static <T extends Collection<String>> T readUtf8Lines(InputStream in, T collection) throws IORuntimeException
T
- 集合类型in
- 输入流collection
- 返回集合IORuntimeException
- IO异常public static <T extends Collection<String>> T readLines(InputStream in, Charset charset, T collection) throws IORuntimeException
T
- 集合类型in
- 输入流charset
- 字符集collection
- 返回集合IORuntimeException
- IO异常public static <T extends Collection<String>> T readLines(Reader reader, T collection) throws IORuntimeException
T
- 集合类型reader
- Reader
collection
- 返回集合IORuntimeException
- IO异常public static void readUtf8Lines(InputStream in, SerConsumer<String> lineHandler) throws IORuntimeException
in
- InputStream
lineHandler
- 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方IORuntimeException
- IO异常public static void readLines(InputStream in, Charset charset, SerConsumer<String> lineHandler) throws IORuntimeException
in
- InputStream
charset
- Charset
编码lineHandler
- 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方IORuntimeException
- IO异常public static void readLines(Reader reader, SerConsumer<String> lineHandler) throws IORuntimeException
reader
- Reader
lineHandler
- 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方IORuntimeException
- IO异常public static FastByteArrayOutputStream readToToken(InputStream in, int token) throws IORuntimeException
in
- 输入流token
- 停止的字符IORuntimeException
- IO异常public static FastByteArrayOutputStream readTo(InputStream in, Predicate<Integer> predicate)
Predicate.test(Object)
in
- 输入流predicate
- 读取结束条件, Predicate.test(Object)
返回true表示结束IORuntimeException
- IO异常public static ByteArrayInputStream toUtf8Stream(String content)
content
- 内容public static ByteArrayInputStream toStream(String content, Charset charset)
content
- 内容charset
- 编码public static InputStream toStream(File file)
InputStream
file
- 文件,非空InputStream
public static InputStream toStream(Path path)
InputStream
path
- Path
,非空InputStream
public static ByteArrayInputStream toStream(byte[] content)
ByteArrayInputStream
content
- 内容bytespublic static ByteArrayInputStream toStream(ByteArrayOutputStream out)
out
- ByteArrayOutputStream
public static ByteArrayInputStream toStream(FastByteArrayOutputStream out)
out
- FastByteArrayOutputStream
public static BufferedInputStream toBuffered(InputStream in)
in
- InputStream
BufferedInputStream
public static BufferedInputStream toBuffered(InputStream in, int bufferSize)
in
- InputStream
bufferSize
- buffer sizeBufferedInputStream
public static BufferedOutputStream toBuffered(OutputStream out)
out
- OutputStream
BufferedOutputStream
public static BufferedOutputStream toBuffered(OutputStream out, int bufferSize)
out
- OutputStream
bufferSize
- buffer sizeBufferedOutputStream
public static BufferedReader toBuffered(Reader reader)
reader
- Reader
BufferedReader
public static BufferedReader toBuffered(Reader reader, int bufferSize)
reader
- Reader
bufferSize
- buffer sizeBufferedReader
public static BufferedWriter toBuffered(Writer writer)
writer
- Writer
BufferedWriter
public static BufferedWriter toBuffered(Writer writer, int bufferSize)
writer
- Writer
bufferSize
- buffer sizeBufferedWriter
public static InputStream toMarkSupport(InputStream in)
in
- 流InputStream
public static PushbackReader toPushBackReader(Reader reader, int pushBackSize)
reader
- 普通ReaderpushBackSize
- 推后的byte数PushbackReader
public static PushbackInputStream toPushbackStream(InputStream in, int pushBackSize)
in
- InputStream
pushBackSize
- 推后的byte数PushbackInputStream
public static InputStream toAvailableStream(InputStream in)
InputStream
转换为InputStream.available()
方法可用的流。InputStream.available()
方法始终为0
InputStream.read()
读取一个字节(未返回会阻塞),一旦读取到了,InputStream.available()
方法就正常了。InputStream.available()
读取到的并非最终长度,而是此次块的长度。in
- 被转换的流PushbackInputStream
public static void writeClose(OutputStream out, byte[] content) throws IORuntimeException
out
- 输出流content
- 写入的内容IORuntimeException
- IO异常public static void write(OutputStream out, byte[] content) throws IORuntimeException
out
- 输出流content
- 写入的内容IORuntimeException
- IO异常public static void write(OutputStream out, boolean isCloseOut, byte[] content) throws IORuntimeException
out
- 输出流isCloseOut
- 写入完毕是否关闭输出流content
- 写入的内容IORuntimeException
- IO异常public static void writeUtf8(OutputStream out, boolean isCloseOut, CharSequence... contents) throws IORuntimeException
out
- 输出流isCloseOut
- 写入完毕是否关闭输出流contents
- 写入的内容,调用toString()方法,不包括不会自动换行IORuntimeException
- IO异常public static void writeStrs(OutputStream out, Charset charset, boolean isCloseOut, CharSequence... contents) throws IORuntimeException
out
- 输出流charset
- 写出的内容的字符集isCloseOut
- 写入完毕是否关闭输出流contents
- 写入的内容,调用toString()方法,不包括不会自动换行IORuntimeException
- IO异常public static void writeObjects(OutputStream out, boolean isCloseOut, Object... contents) throws IORuntimeException
out
- 输出流isCloseOut
- 写入完毕是否关闭输出流contents
- 写入的内容IORuntimeException
- IO异常public static void flush(Flushable flushable)
flushable
- Flushable
public static void closeIfPossible(Object obj)
AutoCloseable
,则调用之obj
- 可关闭对象public static void closeQuietly(AutoCloseable... closeables)
closeables
- 需要关闭的对象public static void nullSafeClose(Closeable closeable) throws IOException
IOException
异常closeable
- 被关闭的对象IOException
- IO异常public static boolean contentEquals(InputStream input1, InputStream input2) throws IORuntimeException
BufferedInputStream
input1
- 第一个流input2
- 第二个流IORuntimeException
- IO异常public static boolean contentEquals(Reader input1, Reader input2) throws IORuntimeException
BufferedInputStream
input1
- 第一个readerinput2
- 第二个readerIORuntimeException
- IO异常public static boolean contentEqualsIgnoreEOL(Reader input1, Reader input2) throws IORuntimeException
BufferedInputStream
input1
- 第一个流input2
- 第二个流IORuntimeException
- IO异常public static LineIter lineIter(Reader reader)
LineIterator it = null; try { it = IoUtil.lineIter(reader); while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { it.close(); }
public static LineIter lineIter(InputStream in, Charset charset)
LineIterator it = null; try { it = IoUtil.lineIter(in, CharsetUtil.CHARSET_UTF_8); while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { it.close(); }
in
- InputStream
charset
- 编码LineIter
public static String toStr(ByteArrayOutputStream out, Charset charset)
ByteArrayOutputStream
转换为Stringout
- ByteArrayOutputStream
charset
- 编码public static int length(InputStream in)
FileInputStream.available()
方法,对于其他流,返回-1in
- 流Copyright © 2025. All rights reserved.