public class IoUtil extends NioUtil
DEFAULT_BUFFER_SIZE, DEFAULT_LARGE_BUFFER_SIZE, DEFAULT_MIDDLE_BUFFER_SIZE, EOF| Constructor and Description |
|---|
IoUtil() |
| Modifier and Type | Method and Description |
|---|---|
static Checksum |
checksum(InputStream in,
Checksum checksum)
计算流的校验码,计算后关闭流
|
static long |
checksumCRC32(InputStream in)
计算流CRC32校验码,计算后关闭流
|
static long |
checksumValue(InputStream in,
Checksum checksum)
计算流的校验码,计算后关闭流
|
static void |
close(Closeable closeable)
关闭
关闭失败不会抛出异常 |
static void |
closeIfPosible(Object obj)
Deprecated.
拼写错误,请使用
closeIfPossible(Object) |
static void |
closeIfPossible(Object obj)
尝试关闭指定对象
判断对象如果实现了 AutoCloseable,则调用之 |
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 BomReader |
getBomReader(InputStream in)
|
static PushbackReader |
getPushBackReader(Reader reader,
int pushBackSize)
|
static BufferedReader |
getReader(BOMInputStream in)
从
BOMInputStream中获取Reader |
static BufferedReader |
getReader(InputStream in,
Charset charset)
获得一个Reader
|
static BufferedReader |
getReader(InputStream in,
String charsetName)
Deprecated.
|
static BufferedReader |
getReader(Reader reader)
|
static BufferedReader |
getUtf8Reader(InputStream in)
获得一个文件读取器,默认使用UTF-8编码
|
static OutputStreamWriter |
getUtf8Writer(OutputStream out)
获得一个Writer,默认编码UTF-8
|
static OutputStreamWriter |
getWriter(OutputStream out,
Charset charset)
获得一个Writer
|
static OutputStreamWriter |
getWriter(OutputStream out,
String charsetName)
Deprecated.
|
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 FastByteArrayOutputStream |
read(InputStream in)
从流中读取内容,读到输出流中,读取完毕后关闭流
|
static FastByteArrayOutputStream |
read(InputStream in,
boolean isClose)
从流中读取内容,读到输出流中,读取完毕后可选是否关闭流
|
static String |
read(InputStream in,
Charset charset)
从流中读取内容,读取完毕后关闭流
|
static String |
read(InputStream in,
String charsetName)
Deprecated.
|
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 String |
readHex64Lower(InputStream in)
从流中读取前64个byte并转换为16进制,字母部分使用小写
|
static String |
readHex64Upper(InputStream in)
从流中读取前64个byte并转换为16进制,字母部分使用大写
|
static String |
readHex8192Upper(InputStream in)
从流中读取前8192个byte并转换为16进制,字母部分使用大写
|
static void |
readLines(InputStream in,
Charset charset,
LineHandler lineHandler)
按行读取数据,针对每行的数据做处理
|
static <T extends Collection<String>> |
readLines(InputStream in,
Charset charset,
T collection)
从流中读取内容
|
static <T extends Collection<String>> |
readLines(InputStream in,
String charsetName,
T collection)
Deprecated.
|
static void |
readLines(Reader reader,
LineHandler lineHandler)
按行读取数据,针对每行的数据做处理
Reader自带编码定义,因此读取数据的编码跟随其编码。 |
static <T extends Collection<String>> |
readLines(Reader reader,
T collection)
从Reader中读取内容
|
static <T> T |
readObj(InputStream in)
从流中读取对象,即对象的反序列化
注意!!!
|
static <T> T |
readObj(InputStream in,
Class<T> clazz)
从流中读取对象,即对象的反序列化,读取后不关闭流
注意!!!
|
static <T> T |
readObj(ValidateObjectInputStream in,
Class<T> clazz)
从流中读取对象,即对象的反序列化,读取后不关闭流
此方法使用了
ValidateObjectInputStream中的黑白名单方式过滤类,用于避免反序列化漏洞通过构造 ValidateObjectInputStream,调用ValidateObjectInputStream.accept(Class[])
或者ValidateObjectInputStream.refuse(Class[])方法添加可以被序列化的类或者禁止序列化的类。 |
static String |
readUtf8(InputStream in)
从流中读取UTF8编码的内容
|
static void |
readUtf8Lines(InputStream in,
LineHandler lineHandler)
按行读取UTF-8编码数据,针对每行的数据做处理
|
static <T extends Collection<String>> |
readUtf8Lines(InputStream in,
T collection)
从流中读取内容,使用UTF-8编码
|
static InputStream |
toAvailableStream(InputStream in)
将指定
InputStream 转换为InputStream.available()方法可用的流。 |
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 |
toMarkSupportStream(InputStream in)
|
static PushbackInputStream |
toPushbackStream(InputStream in,
int pushBackSize)
|
static String |
toStr(ByteArrayOutputStream out,
Charset charset)
ByteArrayOutputStream 转换为String |
static ByteArrayInputStream |
toStream(byte[] content)
byte[] 转为
ByteArrayInputStream |
static ByteArrayInputStream |
toStream(ByteArrayOutputStream out)
|
static FileInputStream |
toStream(File file)
文件转为
FileInputStream |
static ByteArrayInputStream |
toStream(String content,
Charset charset)
String 转为流
|
static ByteArrayInputStream |
toStream(String content,
String charsetName)
Deprecated.
|
static ByteArrayInputStream |
toUtf8Stream(String content)
String 转为UTF-8编码的字节流流
|
static void |
write(OutputStream out,
boolean isCloseOut,
byte[] content)
将byte[]写到流中
|
static void |
write(OutputStream out,
Charset charset,
boolean isCloseOut,
Object... contents)
将多部分内容写到流中,自动转换为字符串
|
static void |
write(OutputStream out,
String charsetName,
boolean isCloseOut,
Object... contents)
Deprecated.
|
static void |
writeObj(OutputStream out,
boolean isCloseOut,
Serializable obj)
将多部分内容写到流中
|
static void |
writeObjects(OutputStream out,
boolean isCloseOut,
Serializable... contents)
将多部分内容写到流中
|
static void |
writeUtf8(OutputStream out,
boolean isCloseOut,
Object... 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 - Readerwriter - WriterbufferSize - 缓存大小count - 最大长度streamProgress - 进度处理器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 - 总拷贝长度streamProgress - 进度条IORuntimeException - IO异常public static long copy(FileInputStream in, FileOutputStream out) throws IORuntimeException
in - 输入out - 输出IORuntimeException - IO异常public static BufferedReader getUtf8Reader(InputStream in)
in - 输入流@Deprecated public static BufferedReader getReader(InputStream in, String charsetName)
getReader(InputStream, Charset)in - 输入流charsetName - 字符集名称public static BufferedReader getReader(BOMInputStream in)
BOMInputStream中获取Readerin - BOMInputStreamBufferedReaderpublic static BomReader getBomReader(InputStream in)
in - InputStreamBomReaderpublic static BufferedReader getReader(InputStream in, Charset charset)
in - 输入流charset - 字符集public static BufferedReader getReader(Reader reader)
reader - 普通Reader,如果为null返回nullBufferedReader or nullpublic static PushbackReader getPushBackReader(Reader reader, int pushBackSize)
reader - 普通ReaderpushBackSize - 推后的byte数PushbackReaderpublic static OutputStreamWriter getUtf8Writer(OutputStream out)
out - 输入流@Deprecated public static OutputStreamWriter getWriter(OutputStream out, String charsetName)
getWriter(OutputStream, Charset)out - 输入流charsetName - 字符集public static OutputStreamWriter getWriter(OutputStream out, Charset charset)
out - 输入流charset - 字符集public static String readUtf8(InputStream in) throws IORuntimeException
in - 输入流IORuntimeException - IO异常@Deprecated public static String read(InputStream in, String charsetName) throws IORuntimeException
read(InputStream, Charset)in - 输入流charsetName - 字符集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 - ReaderisClose - 是否关闭ReaderIORuntimeException - IO异常public static byte[] readBytes(InputStream in) throws IORuntimeException
in - InputStreamIORuntimeException - IO异常public static byte[] readBytes(InputStream in, boolean isClose) throws IORuntimeException
in - InputStreamisClose - 是否关闭输入流IORuntimeException - IO异常public static byte[] readBytes(InputStream in, int length) throws IORuntimeException
in - InputStream,为null返回nulllength - 长度,小于等于0返回空byte数组IORuntimeException - IO异常public static String readHex(InputStream in, int length, boolean toLowerCase) throws IORuntimeException
in - InputStreamlength - 长度toLowerCase - true 传换成小写格式 , false 传换成大写格式IORuntimeException - IO异常public static String readHex64Upper(InputStream in) throws IORuntimeException
in - InputStreamIORuntimeException - IO异常public static String readHex8192Upper(InputStream in) throws IORuntimeException
in - InputStreamIORuntimeException - IO异常public static String readHex64Lower(InputStream in) throws IORuntimeException
in - InputStreamIORuntimeException - IO异常public static <T> T readObj(InputStream in) throws IORuntimeException, UtilException
注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!
T - 读取对象的类型in - 输入流IORuntimeException - IO异常UtilException - ClassNotFoundException包装public static <T> T readObj(InputStream in, Class<T> clazz) throws IORuntimeException, UtilException
注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!
T - 读取对象的类型in - 输入流clazz - 读取对象类型IORuntimeException - IO异常UtilException - ClassNotFoundException包装public static <T> T readObj(ValidateObjectInputStream in, Class<T> clazz) throws IORuntimeException, UtilException
此方法使用了ValidateObjectInputStream中的黑白名单方式过滤类,用于避免反序列化漏洞
通过构造ValidateObjectInputStream,调用ValidateObjectInputStream.accept(Class[])
或者ValidateObjectInputStream.refuse(Class[])方法添加可以被序列化的类或者禁止序列化的类。
T - 读取对象的类型in - 输入流,使用ValidateObjectInputStream中的黑白名单方式过滤类,用于避免反序列化漏洞clazz - 读取对象类型IORuntimeException - IO异常UtilException - ClassNotFoundException包装public static <T extends Collection<String>> T readUtf8Lines(InputStream in, T collection) throws IORuntimeException
T - 集合类型in - 输入流collection - 返回集合IORuntimeException - IO异常@Deprecated public static <T extends Collection<String>> T readLines(InputStream in, String charsetName, T collection) throws IORuntimeException
readLines(InputStream, Charset, Collection)T - 集合类型in - 输入流charsetName - 字符集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 - Readercollection - 返回集合IORuntimeException - IO异常public static void readUtf8Lines(InputStream in, LineHandler lineHandler) throws IORuntimeException
in - InputStreamlineHandler - 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方IORuntimeException - IO异常public static void readLines(InputStream in, Charset charset, LineHandler lineHandler) throws IORuntimeException
in - InputStreamcharset - Charset编码lineHandler - 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方IORuntimeException - IO异常public static void readLines(Reader reader, LineHandler lineHandler) throws IORuntimeException
reader - ReaderlineHandler - 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方IORuntimeException - IO异常@Deprecated public static ByteArrayInputStream toStream(String content, String charsetName)
toStream(String, Charset)content - 内容charsetName - 编码public static ByteArrayInputStream toStream(String content, Charset charset)
content - 内容charset - 编码public static ByteArrayInputStream toUtf8Stream(String content)
content - 内容public static FileInputStream toStream(File file)
FileInputStreamfile - 文件FileInputStreampublic static ByteArrayInputStream toStream(byte[] content)
ByteArrayInputStreamcontent - 内容bytespublic static ByteArrayInputStream toStream(ByteArrayOutputStream out)
out - ByteArrayOutputStreampublic static BufferedInputStream toBuffered(InputStream in)
in - InputStreamBufferedInputStreampublic static BufferedInputStream toBuffered(InputStream in, int bufferSize)
in - InputStreambufferSize - buffer sizeBufferedInputStreampublic static BufferedOutputStream toBuffered(OutputStream out)
out - OutputStreamBufferedOutputStreampublic static BufferedOutputStream toBuffered(OutputStream out, int bufferSize)
out - OutputStreambufferSize - buffer sizeBufferedOutputStreampublic static BufferedReader toBuffered(Reader reader)
reader - ReaderBufferedReaderpublic static BufferedReader toBuffered(Reader reader, int bufferSize)
reader - ReaderbufferSize - buffer sizeBufferedReaderpublic static BufferedWriter toBuffered(Writer writer)
writer - WriterBufferedWriterpublic static BufferedWriter toBuffered(Writer writer, int bufferSize)
writer - WriterbufferSize - buffer sizeBufferedWriterpublic static InputStream toMarkSupportStream(InputStream in)
in - 流InputStreampublic static PushbackInputStream toPushbackStream(InputStream in, int pushBackSize)
in - InputStreampushBackSize - 推后的byte数PushbackInputStreampublic static InputStream toAvailableStream(InputStream in)
InputStream 转换为InputStream.available()方法可用的流。InputStream.available()方法始终为0InputStream.read()读取一个字节(未返回会阻塞),一旦读取到了,InputStream.available()方法就正常了。InputStream.available() 读取到的并非最终长度,而是此次块的长度。in - 被转换的流PushbackInputStreampublic static void write(OutputStream out, boolean isCloseOut, byte[] content) throws IORuntimeException
out - 输出流isCloseOut - 写入完毕是否关闭输出流content - 写入的内容IORuntimeException - IO异常public static void writeUtf8(OutputStream out, boolean isCloseOut, Object... contents) throws IORuntimeException
out - 输出流isCloseOut - 写入完毕是否关闭输出流contents - 写入的内容,调用toString()方法,不包括不会自动换行IORuntimeException - IO异常@Deprecated public static void write(OutputStream out, String charsetName, boolean isCloseOut, Object... contents) throws IORuntimeException
write(OutputStream, Charset, boolean, Object...)out - 输出流charsetName - 写出的内容的字符集isCloseOut - 写入完毕是否关闭输出流contents - 写入的内容,调用toString()方法,不包括不会自动换行IORuntimeException - IO异常public static void write(OutputStream out, Charset charset, boolean isCloseOut, Object... contents) throws IORuntimeException
out - 输出流charset - 写出的内容的字符集isCloseOut - 写入完毕是否关闭输出流contents - 写入的内容,调用toString()方法,不包括不会自动换行IORuntimeException - IO异常public static void writeObj(OutputStream out, boolean isCloseOut, Serializable obj) throws IORuntimeException
out - 输出流isCloseOut - 写入完毕是否关闭输出流obj - 写入的对象内容IORuntimeException - IO异常public static void writeObjects(OutputStream out, boolean isCloseOut, Serializable... contents) throws IORuntimeException
out - 输出流isCloseOut - 写入完毕是否关闭输出流contents - 写入的内容IORuntimeException - IO异常public static void flush(Flushable flushable)
flushable - Flushablepublic static void close(Closeable closeable)
closeable - 被关闭的对象@Deprecated public static void closeIfPosible(Object obj)
closeIfPossible(Object)AutoCloseable,则调用之obj - 可关闭对象public static void closeIfPossible(Object obj)
AutoCloseable,则调用之obj - 可关闭对象public static boolean contentEquals(InputStream input1, InputStream input2) throws IORuntimeException
BufferedInputStreaminput1 - 第一个流input2 - 第二个流IORuntimeException - IO异常public static boolean contentEquals(Reader input1, Reader input2) throws IORuntimeException
BufferedInputStreaminput1 - 第一个readerinput2 - 第二个readerIORuntimeException - IO异常public static boolean contentEqualsIgnoreEOL(Reader input1, Reader input2) throws IORuntimeException
BufferedInputStreaminput1 - 第一个流input2 - 第二个流IORuntimeException - IO异常public static long checksumCRC32(InputStream in) throws IORuntimeException
in - 文件,不能为目录IORuntimeException - IO异常public static Checksum checksum(InputStream in, Checksum checksum) throws IORuntimeException
in - 流checksum - ChecksumIORuntimeException - IO异常public static long checksumValue(InputStream in, Checksum checksum)
in - 流checksum - ChecksumIORuntimeException - 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 - InputStreamcharset - 编码LineIterpublic static String toStr(ByteArrayOutputStream out, Charset charset)
ByteArrayOutputStream 转换为Stringout - ByteArrayOutputStreamcharset - 编码Copyright © 2025. All rights reserved.