public class IoUtil extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static int |
DEFAULT_BUFFER_SIZE
默认缓存大小 8192
|
static int |
DEFAULT_LARGE_BUFFER_SIZE
默认大缓存大小 32768
|
static int |
DEFAULT_MIDDLE_BUFFER_SIZE
默认中等缓存大小 16384
|
static int |
EOF
数据流末尾
|
| 构造器和说明 |
|---|
IoUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static Checksum |
checksum(InputStream in,
Checksum checksum)
计算流的校验码,计算后关闭流
|
static long |
checksumCRC32(InputStream in)
计算流CRC32校验码,计算后关闭流
|
static void |
close(AutoCloseable closeable)
关闭
关闭失败不会抛出异常 |
static void |
close(Closeable closeable)
关闭
关闭失败不会抛出异常 |
static void |
closeIfPosible(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,
StreamProgress streamProgress)
拷贝流,拷贝后不关闭流
|
static long |
copy(ReadableByteChannel in,
WritableByteChannel out)
拷贝流,使用NIO,不会关闭流
|
static long |
copy(ReadableByteChannel in,
WritableByteChannel out,
int bufferSize)
拷贝流,使用NIO,不会关闭流
|
static long |
copy(ReadableByteChannel in,
WritableByteChannel out,
int bufferSize,
StreamProgress streamProgress)
拷贝流,使用NIO,不会关闭流
|
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,
StreamProgress streamProgress)
将Reader中的内容复制到Writer中,拷贝后不关闭Reader
|
static long |
copyByNIO(InputStream in,
OutputStream out,
int bufferSize,
StreamProgress streamProgress)
拷贝流 thanks to: https://github.com/venusdrogon/feilong-io/blob/master/src/main/java/com/feilong/io/IOWriteUtil.java
本方法不会关闭流 |
static void |
flush(Flushable flushable)
从缓存中刷出数据
|
static PushbackReader |
getPushBackReader(Reader reader,
int pushBackSize)
|
static BufferedReader |
getReader(InputStream in,
Charset charset)
获得一个Reader
|
static BufferedReader |
getReader(InputStream in,
String charsetName)
获得一个文件读取器
|
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)
获得一个Writer
|
static String |
read(FileChannel fileChannel,
Charset charset)
从FileChannel中读取内容
|
static String |
read(FileChannel fileChannel,
String charsetName)
从FileChannel中读取内容,读取完毕后并不关闭Channel
|
static FastByteArrayOutputStream |
read(InputStream in)
从流中读取内容,读到输出流中,读取完毕后并不关闭流
|
static String |
read(InputStream in,
Charset charset)
从流中读取内容,读取完毕后并不关闭流
|
static String |
read(InputStream in,
String charsetName)
从流中读取内容
|
static FastByteArrayOutputStream |
read(ReadableByteChannel channel)
从流中读取内容,读到输出流中
|
static String |
read(ReadableByteChannel channel,
Charset charset)
从流中读取内容,读取完毕后并不关闭流
|
static String |
read(Reader reader)
从Reader中读取String,读取完毕后并不关闭Reader
|
static byte[] |
readBytes(InputStream in)
从流中读取bytes,读取完毕后关闭流
|
static byte[] |
readBytes(InputStream in,
boolean isCloseStream)
从流中读取bytes
|
static byte[] |
readBytes(InputStream in,
int length)
读取指定长度的byte数组,不关闭流
|
static String |
readHex(InputStream in,
int length,
boolean toLowerCase)
读取16进制字符串
|
static String |
readHex28Lower(InputStream in)
从流中读取前28个byte并转换为16进制,字母部分使用小写
|
static String |
readHex28Upper(InputStream in)
从流中读取前28个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)
从流中读取内容
|
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)
已过时。
由于存在对象反序列化漏洞风险,请使用
readObj(InputStream, Class) |
static <T> T |
readObj(InputStream in,
Class<T> clazz)
从流中读取对象,即对象的反序列化,读取后不关闭流
|
static String |
readUtf8(FileChannel fileChannel)
从FileChannel中读取UTF-8编码内容
|
static void |
readUtf8Lines(InputStream in,
LineHandler lineHandler)
按行读取UTF-8编码数据,针对每行的数据做处理
|
static <T extends Collection<String>> |
readUtf8Lines(InputStream in,
T collection)
从流中读取内容,使用UTF-8编码
|
static BufferedInputStream |
toBuffered(InputStream in)
|
static BufferedOutputStream |
toBuffered(OutputStream out)
|
static InputStream |
toMarkSupportStream(InputStream in)
|
static PushbackInputStream |
toPushbackStream(InputStream in,
int pushBackSize)
|
static ByteArrayInputStream |
toStream(byte[] content)
String 转为流
|
static FileInputStream |
toStream(File file)
文件转为流
|
static ByteArrayInputStream |
toStream(String content,
Charset charset)
String 转为流
|
static ByteArrayInputStream |
toStream(String content,
String charsetName)
String 转为流
|
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)
将多部分内容写到流中,自动转换为字符串
|
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 final int DEFAULT_BUFFER_SIZE
public static final int DEFAULT_MIDDLE_BUFFER_SIZE
public static final int DEFAULT_LARGE_BUFFER_SIZE
public static final int EOF
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(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 copyByNIO(InputStream in, OutputStream out, int bufferSize, StreamProgress streamProgress) throws IORuntimeException
in - 输入流out - 输出流bufferSize - 缓存大小streamProgress - 进度条IORuntimeException - IO异常public static long copy(FileInputStream in, FileOutputStream out) throws IORuntimeException
in - 输入out - 输出IORuntimeException - IO异常public static long copy(ReadableByteChannel in, WritableByteChannel out) throws IORuntimeException
in - ReadableByteChannelout - WritableByteChannelIORuntimeException - IO异常public static long copy(ReadableByteChannel in, WritableByteChannel out, int bufferSize) throws IORuntimeException
in - ReadableByteChannelout - WritableByteChannelbufferSize - 缓冲大小,如果小于等于0,使用默认IORuntimeException - IO异常public static long copy(ReadableByteChannel in, WritableByteChannel out, int bufferSize, StreamProgress streamProgress) throws IORuntimeException
in - ReadableByteChannelout - WritableByteChannelbufferSize - 缓冲大小,如果小于等于0,使用默认streamProgress - StreamProgress进度处理器IORuntimeException - IO异常public static BufferedReader getUtf8Reader(InputStream in)
in - 输入流public static BufferedReader getReader(InputStream in, String charsetName)
in - 输入流charsetName - 字符集名称public 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 - 输入流public static OutputStreamWriter getWriter(OutputStream out, String charsetName)
out - 输入流charsetName - 字符集public static OutputStreamWriter getWriter(OutputStream out, Charset charset)
out - 输入流charset - 字符集public static String read(InputStream in, String charsetName) throws IORuntimeException
in - 输入流charsetName - 字符集IORuntimeException - IO异常public static String read(InputStream in, Charset charset) throws IORuntimeException
in - 输入流,读取完毕后并不关闭流charset - 字符集IORuntimeException - IO异常public static String read(ReadableByteChannel channel, Charset charset) throws IORuntimeException
channel - 可读通道,读取完毕后并不关闭通道charset - 字符集IORuntimeException - IO异常public static FastByteArrayOutputStream read(InputStream in) throws IORuntimeException
in - 输入流IORuntimeException - IO异常public static FastByteArrayOutputStream read(ReadableByteChannel channel) throws IORuntimeException
channel - 可读通道,读取完毕后并不关闭通道IORuntimeException - IO异常public static String read(Reader reader) throws IORuntimeException
reader - ReaderIORuntimeException - IO异常public static String readUtf8(FileChannel fileChannel) throws IORuntimeException
fileChannel - 文件管道IORuntimeException - IO异常public static String read(FileChannel fileChannel, String charsetName) throws IORuntimeException
fileChannel - 文件管道charsetName - 字符集IORuntimeException - IO异常public static String read(FileChannel fileChannel, Charset charset) throws IORuntimeException
fileChannel - 文件管道charset - 字符集IORuntimeException - IO异常public static byte[] readBytes(InputStream in) throws IORuntimeException
in - InputStreamIORuntimeException - IO异常public static byte[] readBytes(InputStream in, boolean isCloseStream) throws IORuntimeException
in - InputStreamisCloseStream - 是否关闭输入流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 readHex28Upper(InputStream in) throws IORuntimeException
in - InputStreamIORuntimeException - IO异常public static String readHex28Lower(InputStream in) throws IORuntimeException
in - InputStreamIORuntimeException - IO异常@Deprecated public static <T> T readObj(InputStream in) throws IORuntimeException, UtilException
readObj(InputStream, Class)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 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, String charsetName, T collection) throws IORuntimeException
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自带编码定义,因此读取数据的编码跟随其编码。reader - ReaderlineHandler - 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方IORuntimeException - IO异常public static ByteArrayInputStream toStream(String content, String charsetName)
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)
file - 文件FileInputStreampublic static ByteArrayInputStream toStream(byte[] content)
content - 内容bytespublic static BufferedInputStream toBuffered(InputStream in)
in - InputStreamBufferedInputStreampublic static BufferedOutputStream toBuffered(OutputStream out)
out - OutputStreamBufferedOutputStreampublic static InputStream toMarkSupportStream(InputStream in)
in - 流InputStreampublic static PushbackInputStream toPushbackStream(InputStream in, int pushBackSize)
in - InputStreampushBackSize - 推后的byte数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异常public static void write(OutputStream out, String charsetName, boolean isCloseOut, Object... contents) throws IORuntimeException
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 - 被关闭的对象public static void close(AutoCloseable closeable)
closeable - 被关闭的对象public static void closeIfPosible(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异常Copyright © 2020. All rights reserved.