IOUtils总结

结合源码,看看IOUTils都有什么用处吧!
代码参考https://github.com/xinghalo/JDK-Learning


常用的静态变量

在IOUtils中还是有很多常用的一些变量的,比如换行符等等


public static final char DIR_SEPARATOR_UNIX = '/';
public static final char DIR_SEPARATOR_WINDOWS = '\\';
public static final char DIR_SEPARATOR;
public static final String LINE_SEPARATOR_UNIX = "\n";
public static final String LINE_SEPARATOR_WINDOWS = "\r\n";
public static final String LINE_SEPARATOR;


static {
    DIR_SEPARATOR = File.separatorChar;

    StringBuilderWriter buf = new StringBuilderWriter(4);
    PrintWriter out = new PrintWriter(buf);
    out.println();
    LINE_SEPARATOR = buf.toString();
    out.close();
}

常用方法

copy

这个方法可以拷贝流,算是这个工具类中使用最多的方法了。支持多种数据间的拷贝:

copy(inputstream,outputstream)
copy(inputstream,writer)
copy(inputstream,writer,encoding)
copy(reader,outputstream)
copy(reader,writer)
copy(reader,writer,encoding)

copy内部使用的其实还是copyLarge方法。因为copy能拷贝Integer.MAX_VALUE的字节数据,即2^31-1。

copyLarge

这个方法适合拷贝较大的数据流,比如2G以上。

copyLarge(reader,writer) 默认会用1024*4的buffer来读取
copyLarge(reader,writer,buffer)

内部的细节可以参考:

 public static long copyLarge(Reader input, Writer output, char [] buffer) throws IOException {
        long count = 0;
        int n = 0;
        while (EOF != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }

这个方法会用一个固定大小的Buffer,持续不断的读取数据,然后写入到输出流中。

read

从一个流中读取内容

read(inputstream,byte[])
read(inputstream,byte[],offset,length) 
//offset是buffer的偏移值,length是读取的长度

read(reader,char[])
read(reader,char[],offset,length)

这里我写了个小例子,可以测试read方法的效果:

@Test
    public void readTest(){
        try{
            byte[] bytes = new byte[4];
            InputStream is = IOUtils.toInputStream("hello world");
            IOUtils.read(is, bytes);
            System.out.println(new String(bytes));

            bytes = new byte[10];
            is = IOUtils.toInputStream("hello world");
            IOUtils.read(is, bytes, 2, 4);
            System.out.println(new String(bytes));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

得到的结果是:

hell
□□hell□□□□
readFully

这个方法会读取指定长度的流,如果读取的长度不够,就会抛出异常

readFully(inputstream,byte[])
readFully(inputstream,byte[],offset,length)
readFully(reader,charp[])
readFully(reader,char[],offset,length)

比如:

@Test
    public void readFullyTest(){
        byte[] bytes = new byte[4];
        InputStream is  = IOUtils.toInputStream("hello world");
        try {
            IOUtils.readFully(is,bytes);
            System.out.println(new String(bytes));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

输出

hell

但是如果读取20个byte,就会出错了

java.io.EOFException: Length to read: 20 actual: 11
    at org.apache.commons.io.IOUtils.readFully(IOUtils.java:2539)
    at org.apache.commons.io.IOUtils.readFully(IOUtils.java:2558)
    at test.java.IOUtilsTest.readFullyTest(IOUtilsTest.java:22)
    ...
readLines

readLines方法可以从流中读取内容,并转换为String的list

readLines(inputstream)
readLines(inputstream,charset)
readLines(inputstream,encoding)
readLines(reader)

这个方法极大简化了之前原始的读取方法:

 @Test
    public void readLinesTest(){
        try{
            InputStream is = new FileInputStream("D://test1.txt");
            List<String> lines = IOUtils.readLines(is);
            for(String line : lines){
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

输出内容:

hello
world

nihao
ioutils
skip

这个方法用于跳过指定长度的流,

skip(inputstream,skip_length)
skip(ReadableByteChannel,skip_length)
skip(reader,skip_length)

例如:

@Test
    public void skipTest(){
        InputStream is = IOUtils.toInputStream("hello world");
        try {
            IOUtils.skip(is,4);
            System.out.println(IOUtils.toString(is,"utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
skipFully

这个方法类似skip,只是如果忽略的长度大于现有的长度,就会抛出异常

skipFully(inputstream,toSkip)
skipFully(readableByteChannel,toSkip)
skipFully(inputstream,toSkip)

例如

@Test
    public void skipFullyTest(){
        InputStream is = IOUtils.toInputStream("hello world");
        try {
            IOUtils.skipFully(is,30);
            System.out.println(IOUtils.toString(is,"utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
write

这个方法可以把数据写入到输出流中

write(byte[] data, OutputStream output)
write(byte[] data, Writer output)
write(byte[] data, Writer output, Charset encoding)
write(byte[] data, Writer output, String encoding)
write(char[] data, OutputStream output)
write(char[] data, OutputStream output, Charset encoding)
write(char[] data, OutputStream output, String encoding)
write(char[] data, Writer output)
write(CharSequence data, OutputStream output)
write(CharSequence data, OutputStream output, Charset encoding)
write(CharSequence data, OutputStream output, String encoding)
write(CharSequence data, Writer output)
write(StringBuffer data, OutputStream output)
write(StringBuffer data, OutputStream output, String encoding)
write(StringBuffer data, Writer output)
write(String data, OutputStream output)
write(String data, OutputStream output, Charset encoding)
write(String data, OutputStream output, String encoding)
write(String data, Writer output)

例如

@Test
    public void writeTest(){
        try {
            OutputStream os = new FileOutputStream("E:/test.txt");
            IOUtils.write("hello write!",os);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
writeLines

这个方法可以把string的List写入到输出流中

writeLines(Collection<?> lines, String lineEnding, OutputStream output)
writeLines(Collection<?> lines, String lineEnding, OutputStream output, Charset encoding)
writeLines(Collection<?> lines, String lineEnding, OutputStream output, String encoding)
writeLines(Collection<?> lines, String lineEnding, Writer writer)

例如

@Test
    public void writeLinesTest() throws IOException {
        List<String> lines = new ArrayList();
        lines.add("hello");
        lines.add("list");
        lines.add("to");
        lines.add("file");
        OutputStream os = new FileOutputStream("E:/test.txt");
        IOUtils.writeLines(lines,IOUtils.LINE_SEPARATOR,os);
    }
close

关闭URL连接

close(URLConnection conn)
closeQuietly

忽略nulls和异常,关闭某个流

close(URLConnection conn)
closeQuietly(Closeable... closeables)
closeQuietly(Closeable closeable)
closeQuietly(InputStream input)
closeQuietly(OutputStream output)
closeQuietly(Reader input)
closeQuietly(Selector selector)
closeQuietly(ServerSocket sock)
closeQuietly(Socket sock)
closeQuietly(Writer output)
contentEquals

比较两个流是否相同

contentEquals(InputStream input1, InputStream input2)
contentEquals(Reader input1, Reader input2)

例如

@Test
    public void contentEqualsTest(){
        InputStream is1 = IOUtils.toInputStream("hello123");
        InputStream is2 = IOUtils.toInputStream("hello123");

        try {
            System.out.println(IOUtils.contentEquals(is1,is2));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
contentEqualsIgnoreEOL

比较两个流,忽略换行符

contentEqualsIgnoreEOL(Reader input1, Reader input2)
lineIterator

读取流,返回迭代器

lineIterator(InputStream input, Charset encoding)
lineIterator(InputStream input, String encoding)
lineIterator(Reader reader)
toBufferedInputStream

把流的全部内容放在另一个流中

toBufferedInputStream(InputStream input)
toBufferedInputStream(InputStream input, int size)
toBufferedReader

返回输入流

toBufferedReader(Reader reader)
toBufferedReader(Reader reader, int size)
toByteArray

返回字节数组

toByteArray(InputStream input)
toByteArray(InputStream input, int size)
toByteArray(InputStream input, long size)
toByteArray(Reader input)
toByteArray(Reader input, Charset encoding)
toByteArray(Reader input, String encoding)
toByteArray(String input)
toByteArray(URI uri)
toByteArray(URL url)
toByteArray(URLConnection urlConn)
toCharArray

返回字符数组

toCharArray(InputStream is)
toCharArray(InputStream is, Charset encoding)
toCharArray(InputStream is, String encoding)
toCharArray(Reader input)
toInputStream

返回输入流

toInputStream(CharSequence input)
toInputStream(CharSequence input, Charset encoding)
toInputStream(CharSequence input, String encoding)
toInputStream(String input)
toInputStream(String input, Charset encoding)
toInputStream(String input, String encoding)
toString

返回字符串

toString(byte[] input)
toString(byte[] input, String encoding)
toString(InputStream input)
toString(InputStream input, Charset encoding)
toString(InputStream input, String encoding)
toString(Reader input)
toString(URI uri)
toString(URI uri, Charset encoding)
toString(URI uri, String encoding)
toString(URL url)
toString(URL url, Charset encoding)
toString(URL url, String encoding)

本文转自与:【http://www.cnblogs.com/xing901022/p/5978989.html】
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: "java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.toByteArray" 意味着在程序运行时,找不到类 "org.apache.poi.util.IOUtils" 中的方法 "toByteArray"。这可能是因为程序需要的 POI 版本与已安装的版本不匹配或者是因为依赖缺失导致的。建议检查程序的依赖配置,确保所有需要的 POI 版本都已安装。 ### 回答2: java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.toByteArray是Java运行时异常,表示在调用某个方法时,JVM无法找到指定的方法。 该异常通常发生在使用Apache POI库进行文件读取和写入时。在这种情况下,错误的原因可能是版本不匹配或依赖关系不正确。 要解决这个问题,可以尝试以下几种方法: 1. 检查Apache POI的版本:确保使用的Apache POI版本与当前项目的其他依赖项兼容。如果版本不一致,可能会导致该错误。 2. 更新依赖项:如果已经使用较旧的Apache POI版本,请尝试更新依赖项以使用最新版本的Apache POI。 3. 清理和构建项目:有时,旧的编译文件和缓存可能会导致错误。尝试清理和重新构建项目,以确保所有文件都是最新的。 4. 检查类路径:确保所有必需的库和依赖项都正确地包含在类路径中,并且没有重复的库存在。 5. 阅读文档和错误日志:查阅Apache POI的文档和错误日志,以获取更多关于该异常的特定信息和解决方案。 请注意,以上方法只是一些常见的解决方案,具体解决方法可能因实际情况而异。如果问题仍然存在,可能需要进一步调查和排查该异常的具体原因。 ### 回答3: java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.toByteArray的错误是指在Java代码中调用了org.apache.poi.util.IOUtils工具类的toByteArray方法, 但在运行时未找到该方法。 这种错误通常是由于版本不匹配所引起的。原因可能是代码中使用的poi库版本与正在运行的poi库版本不兼容。为了解决该问题,可以尝试以下几种方法: 1. 检查代码中使用的poi库版本是否正确。可以查看pom.xml文件或使用的构建工具配置文件来确定使用的poi版本。确保代码中使用的poi版本与正在运行的poi版本一致。 2. 检查项目的依赖关系。如果使用了其他库,这些库可能与poi库版本冲突。可以通过更新依赖库版本或去除冲突的依赖来解决问题。 3. 清除缓存并重新编译项目。有时旧版本的库仍然存在于编译后的二进制文件中,导致错误。在清除缓存后,重新编译项目可以确保使用最新的库版本。 4. 如果以上方法都无效,可以尝试使用不同版本的poi库。可以在代码中更改poi的版本,然后重新编译和运行项目。 总结,java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.toByteArray错误是由于代码中调用的poi库版本与正在运行的poi库版本不匹配引起的。可以通过检查版本、解决依赖冲突、清除缓存或更改版本等方法来解决该问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值